﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Web;
using ShareMarking.Service;
using ShareMarking.Entity;

namespace ShareMarking.Interface
{
    public class NC_Sync
    {
        //protected string httpstr = "http://ncsearch.baiytfp.com";  //正式
        //protected string httpstr = "http://nc.baiytfp.com";
        //protected string access_key_id = "xiaoma";
        //protected string access_key_secret = "xiaoma_secret";

        protected string httpstr;
        protected string access_key_id;
        protected string access_key_secret;

        public NC_Sync(string httpstr, string keyid, string keysecret)
        {
            this.httpstr = httpstr;
            this.access_key_id = keyid;
            this.access_key_secret = keysecret;
        }

        public int GetNCCount()
        {
            #region sign
            var timestamp = DateTime.Now.Ticks - 621355968000000000;
            var parameters = new Dictionary<string, string>();
            parameters.Add("access_key_id", access_key_id);
            parameters.Add("timestamp", timestamp.ToString());
            var request_str = BuildQuery(parameters);
            var signature = HMACSHA1Encrypt(request_str, access_key_secret);
            #endregion
            parameters.Add("signature", signature);
            var postData = Encoding.UTF8.GetBytes(BuildQuery(parameters));
            ComLib.WebClientHelper click = new ComLib.WebClientHelper();
            click.Encoding = Encoding.UTF8;
            //string result = click.Post(this.httpstr + "/ncsearch/ncapi/nc_count.do", postData);
            string result = click.Post(this.httpstr + "/NcSyncApi/GetNcCount", postData);
            var resultentity = ComLib.JsonHelper.JsonDeserialize<ShareMarking.Interface.nccountresults>(result);
            //return resultentity.result;
            return resultentity.result.nc_count;
        }

        public ShareMarking.Interface.ncresults GetNC(int currentpage, int gopage, int pagesize, int rownum)
        {
            #region sign
            var timestamp = DateTime.Now.Ticks - 621355968000000000;
            var parameters = new Dictionary<string, string>();
            parameters.Add("access_key_id", access_key_id);
            parameters.Add("currentpage", currentpage.ToString());
            parameters.Add("gopage", gopage.ToString());
            parameters.Add("name-value", "");
            parameters.Add("pagesize", pagesize.ToString());
            parameters.Add("rownum", rownum.ToString());
            parameters.Add("timestamp", timestamp.ToString());
            var request_str = BuildQuery(parameters);
            var signature = HMACSHA1Encrypt(request_str, access_key_secret);
            #endregion

            #region 请求
            parameters.Add("signature", signature);
            var postData = Encoding.UTF8.GetBytes(BuildQuery(parameters));
            ComLib.WebClientHelper click = new ComLib.WebClientHelper();
            click.Encoding = Encoding.UTF8;
            //string result = click.Post(httpstr + "/ncsearch/ncapi/nc_list.do", postData);
            string result = click.Post(httpstr + "/NcSyncApi/GetNcData", postData);
            var resultentity = ComLib.JsonHelper.JsonDeserialize<ShareMarking.Interface.ncresults>(result);
            #endregion

            return resultentity;
        }

        /// <summary>
        /// 组装普通文本请求参数。
        /// </summary>
        /// <param name="parameters">Key-Value形式请求参数字典</param>
        /// <returns>URL编码后的请求数据</returns>
        public static string BuildQuery(IDictionary<string, string> parameters)
        {
            StringBuilder postData = new StringBuilder();
            bool hasParam = false;

            IEnumerator<KeyValuePair<string, string>> dem = parameters.GetEnumerator();
            while (dem.MoveNext())
            {
                string name = dem.Current.Key;
                string value = dem.Current.Value;
                // 忽略参数名为空的参数
                if (!string.IsNullOrEmpty(name))
                {
                    if (hasParam)
                    {
                        postData.Append("&");
                    }

                    postData.Append(HttpUtility.UrlEncode(name, Encoding.UTF8));
                    postData.Append("=");
                    postData.Append(HttpUtility.UrlEncode(value, Encoding.UTF8));
                    hasParam = true;
                }
            }

            return postData.ToString();
        }

        public static string HMACSHA1Encrypt(string EncryptText, string EncryptKey)
        {
            //HMACSHA1 myHMACSHA1 = new HMACSHA1(Encoding.UTF8.GetBytes(EncryptKey));
            //byte[] RstRes = myHMACSHA1.ComputeHash(Encoding.UTF8.GetBytes(EncryptText));
            //StringBuilder EnText = new StringBuilder();
            //foreach (byte Byte in RstRes)
            //{
            //    EnText.AppendFormat("{0:x2}", Byte);
            //}
            //return EnText.ToString();
            HMACSHA1 hmacsha1 = new HMACSHA1();
            hmacsha1.Key = Encoding.UTF8.GetBytes(EncryptKey);
            byte[] dataBuffer = Encoding.UTF8.GetBytes(EncryptText);
            byte[] hashBytes = hmacsha1.ComputeHash(dataBuffer);
            return Convert.ToBase64String(hashBytes);
        }
    }

    public class NC_QRCode
    {
        protected string httpstr;
        protected string signserect;

        public NC_QRCode(string httpstr, string signserect)
        {
            this.httpstr = httpstr;
            this.signserect = signserect;
        }
        /// <summary>
        /// NC招募二维码
        /// </summary>
        public void Sync_QrCode()
        {
            //先得到所有的需要更新二维码的NC
            var nclist = nc_ncuserService.Instance.Get_Entity_byWhere(Entity.nc_ncuser._.isneedupdate == true, null);
            ComLib.LogLib.Log4NetBase.Log("3、==========================更新NC二维码===========数量:" + nclist.Count);
            nclist.ForEach(o =>
            {
                try
                {
                    #region 操作NC/NCMM二维码
                    if (!Sync(o))
                    {
                        ComLib.LogLib.Log4NetBase.Log("操作NC招募二维码失败::::NC_Code:" + o.nc_code);
                    }                  
                    if (!SyncMM(o))
                    {
                        ComLib.LogLib.Log4NetBase.Log("操作NC妈妈班二维码失败::::NC_Code:" + o.nc_code);
                    }
                    else
                    {
                        #region 将nc置为已修改
                        o.isneedupdate = false;
                        nc_ncuserService.Instance.Update_Entity(o);
                        #endregion
                    }
                    #endregion
                    
                }
                catch (Exception ex)
                {
                    ComLib.LogLib.Log4NetBase.Log("更新一个二维码错误::::" + ex.Message + ex.StackTrace);
                }
            });
        }
       
        /// <summary>
        /// NC招募二维码
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public bool Sync(Entity.nc_ncuser o)
        {
            bool issucess = false;
            if (o.state == "活跃员工" || o.state == "休眠员工")
            {
                #region 活跃员工的处理 qr_Type=1

                var has = qr_QrCodeRelateService.Instance.Get_IsExist_ByWhere(
                    qr_QrCodeRelate._.qr_Type == 1 && qr_QrCodeRelate._.IsDelete == false
                    && qr_QrCodeRelate._.rel_Code == o.nc_code);

                if (!has)
                {
                    #region 如果在关联表没有NC对应的记录，就新增二维码

                    //查询是否有以前弃用的二维码
                    var oldqr = qr_QrCodeService.Instance.Get_Entitys_ByTop(1, qr_QrCode._.type == 1 &&
                        qr_QrCode._.isdelete == true, qr_QrCode._.scene_id.Asc);

                    if (oldqr.IsNullOrCountLTE0())
                    {
                        #region 使用新的

                        #region get sceneid
                        var sceneid = 0;
                        qr_QrCodeType type = qr_QrCodeTypeService.Instance.Get_SingleEntity_byWhere(qr_QrCodeType._.QrCodeTypeID == 1);
                        sceneid = type.QT_CurrentIndex.ConvertTo<int>() + 1;
                        #endregion

                        #region 编辑NC招募图文信息
                        var extra = new List<extra>() { 
                                new extra { 
                                    title = "马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇", 
                                    shortdesc = "一键注册，终身礼遇", 
                                    linkurl = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode="+o.nc_code+"$openId=####", 
                                    picurl = "http://admin.mjn.howjoin.com/images/p1.jpg" 
                                },
                                new extra{
                                    title = "不可错过的海量专业孕育课程＆胎教早教资源！", 
                                    shortdesc = "有声资源每周更新，满足妈妈＆宝宝共同需求", 
                                    linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=2$sn=0d2328393e16964b17e11e30bb85378f$scene=1$srcid=06209qW7BXxURv3bBLvzMGHZ", 
                                    picurl = "http://admin.mjn.howjoin.com/images/p2.jpg" 
                                },
                                new extra{
                                    title = "【会员福利】奶粉、京东优惠券、巧虎礼包、滴滴红包等你拿,还有……", 
                                    shortdesc = "惊喜连连，准备好了么？", 
                                    linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=3$sn=8478a978359403d68c1452f132455d32$scene=1$srcid=0620AZL0kIEOrjKTGX6Y2emW", 
                                    picurl = "http://admin.mjn.howjoin.com/images/p3.jpg" 
                                }
                            };
                        var message = new message()
                        {
                            title = o.name,
                            remark = o.name + "的二维码",
                            sacode = "test",
                            scene_id = sceneid,
                            extra = extra
                        };
                        #endregion

                        #region 请求vigo 生成二维码
                        var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
                        var resultentity = vigo.CreateQrcode(message);
                        #endregion

                        while (resultentity.errcode == "12")
                        {
                            ComLib.LogLib.Log4NetBase.Log("二维码sceneid重复" + sceneid);
                            sceneid += 1;
                            message.scene_id = sceneid;
                            resultentity = vigo.CreateQrcode(message);
                        }

                        #region 将返回的信息保存
                        if (resultentity.errcode == "00")
                        {
                            #region 保存二维码
                            qr_QrCode qrcode = new qr_QrCode()
                            {
                                id = resultentity.message.id,
                                type = 1,
                                scene_id = resultentity.message.scene_id,
                                sacode = resultentity.message.sacode,
                                title = resultentity.message.title,
                                remark = resultentity.message.remark,
                                code_url = resultentity.message.code_url,
                                qrcode_url = resultentity.message.qrcode_url,
                                addtime = resultentity.message.addtime,
                                status = resultentity.message.status,
                                DateModified = DateTime.Now,
                                wx_title = "【美赞臣】微信注册",
                                wx_content = "注册成为会员，享受更多优惠折扣！",
                                wx_url = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode=" + o.nc_code + "$openId=####",
                                wx_imgurl = "http://admin.mjn.howjoin.com/images/logo_b.png",
                                isdelete = false
                            };
                            issucess = qr_QrCodeService.Instance.Add_Entity(qrcode);
                            #endregion

                            #region 添加关联
                            qr_QrCodeRelate qrrelate = new qr_QrCodeRelate()
                            {
                                qr_Type = 1,//1是NC
                                qr_CodeID = resultentity.message.id,
                                qr_Scene_ID = resultentity.message.scene_id,
                                rel_Code = o.nc_code,
                                IsDelete = false,
                                CreateOn = DateTime.Now,
                                Chre_Source = "188",//20170612需求新增子来源、指定代码字段
                                Appoint_Code = o.nc_code
                            };
                            var isaqrrelate = qr_QrCodeRelateService.Instance.Add_Entity(qrrelate);
                            #endregion

                            #region 更改二维码类型表
                            type.QT_CurrentIndex = sceneid;
                            type.QT_Used = type.QT_Used + 1;
                            type.QT_Surplus = type.QT_Surplus - 1;
                            var istype = qr_QrCodeTypeService.Instance.Update_Entity(type);
                            #endregion                            
                        }
                        #endregion

                        ComLib.LogLib.Log4NetBase.Log("3、====新增NC二维码========新增===NCCode:" + o.nc_code + "::==sceneid==" + resultentity.message.scene_id);
                        #endregion
                    }
                    else
                    {
                        #region 使用弃用的

                        var qr = oldqr[0];

                        #region 更新二维码

                        #region 编辑图文信息
                        var extra = new List<extra>() { 
                                    new extra { 
                                        title = "马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇", 
                                        shortdesc = "一键注册，终身礼遇", 
                                        linkurl = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode="+o.nc_code+"$openId=####", 
                                        picurl = "http://admin.mjn.howjoin.com/images/p1.jpg" 
                                    },
                                    new extra{
                                        title = "不可错过的海量专业孕育课程＆胎教早教资源！", 
                                        shortdesc = "有声资源每周更新，满足妈妈＆宝宝共同需求", 
                                        linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=2$sn=0d2328393e16964b17e11e30bb85378f$scene=1$srcid=06209qW7BXxURv3bBLvzMGHZ", 
                                        picurl = "http://admin.mjn.howjoin.com/images/p2.jpg" 
                                    },
                                    new extra{
                                        title = "【会员福利】奶粉、京东优惠券、巧虎礼包、滴滴红包等你拿,还有……", 
                                        shortdesc = "惊喜连连，准备好了么？", 
                                        linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=3$sn=8478a978359403d68c1452f132455d32$scene=1$srcid=0620AZL0kIEOrjKTGX6Y2emW", 
                                        picurl = "http://admin.mjn.howjoin.com/images/p3.jpg" 
                                    }
                                };
                        var message = new message2()
                        {
                            id = qr.id.ToString(),
                            title = o.name,
                            remark = o.name + "的二维码",
                            sacode = o.name + "的二维码",
                            extra = extra
                        };
                        #endregion

                        var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
                        var result = vigo.UpdateQrcode(message);

                        #region 修改弃用的二维码状态为启用
                        qr.title = o.name;
                        qr.remark = o.name + "的二维码";
                        qr.isdelete = false;
                        qr.DateModified = DateTime.Now;
                        qr.wx_title = "【美赞臣】微信注册";
                        qr.wx_content = "注册成为会员，享受更多优惠折扣！";
                        qr.wx_url = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode=" + o.nc_code + "$openId=####";
                        qr.wx_imgurl = "http://admin.mjn.howjoin.com/images/logo_b.png";
                        issucess=qr_QrCodeService.Instance.Update_Entity(qr);
                        #endregion

                        #endregion

                        #region 新建关联
                        qr_QrCodeRelate qrrelate = new qr_QrCodeRelate()
                        {
                            qr_Type = 1,//1是NC
                            qr_CodeID = qr.id,
                            qr_Scene_ID = qr.scene_id,
                            rel_Code = o.nc_code,
                            IsDelete = false,
                            CreateOn = DateTime.Now,
                            Chre_Source = "188",//20170612需求新增子来源、指定代码字段
                            Appoint_Code = o.nc_code
                        };
                        qr_QrCodeRelateService.Instance.Add_Entity(qrrelate);
                        #endregion

                        ComLib.LogLib.Log4NetBase.Log("3、====NC为活跃，使用旧的二维码：：：NCCode：：" + o.nc_code + "::==sceneid==::" + qrrelate.qr_Scene_ID);

                        #endregion
                    }
                    #endregion
                }
                else
                {
                    #region 如果有了二维码，也更新一次
                    var relate = qr_QrCodeRelateService.Instance.Get_SingleEntity_byWhere(
                    qr_QrCodeRelate._.qr_Type == 1 && qr_QrCodeRelate._.IsDelete == false
                    && qr_QrCodeRelate._.rel_Code == o.nc_code);

                    if (relate != null)
                    {
                        var qrcode = qr_QrCodeService.Instance.Get_SingleEntity_byWhere(qr_QrCode._.id == relate.qr_CodeID);
                        if (qrcode != null)
                        {
                            #region 更新二维码

                            #region 编辑图文信息
                            var extra = new List<extra>() { 
                                        new extra { 
                                            title = "马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇", 
                                            shortdesc = "一键注册，终身礼遇", 
                                            linkurl = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode="+o.nc_code+"$openId=####", 
                                            picurl = "http://admin.mjn.howjoin.com/images/p1.jpg" 
                                        },
                                        new extra{
                                            title = "不可错过的海量专业孕育课程＆胎教早教资源！", 
                                            shortdesc = "有声资源每周更新，满足妈妈＆宝宝共同需求", 
                                            linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=2$sn=0d2328393e16964b17e11e30bb85378f$scene=1$srcid=06209qW7BXxURv3bBLvzMGHZ", 
                                            picurl = "http://admin.mjn.howjoin.com/images/p2.jpg" 
                                        },
                                        new extra{
                                            title = "【会员福利】奶粉、京东优惠券、巧虎礼包、滴滴红包等你拿,还有……", 
                                            shortdesc = "惊喜连连，准备好了么？", 
                                            linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=3$sn=8478a978359403d68c1452f132455d32$scene=1$srcid=0620AZL0kIEOrjKTGX6Y2emW", 
                                            picurl = "http://admin.mjn.howjoin.com/images/p3.jpg" 
                                        }
                                    };
                            var message = new message2()
                            {
                                id = qrcode.id.ToString(),
                                title = o.name,
                                remark = o.name + "的二维码",
                                sacode = o.name + "的二维码",
                                extra = extra
                            };
                            #endregion

                            var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
                            var result = vigo.UpdateQrcode(message);
                            if (result.IndexOf("成功")>0)
                            {
                                #region 修改关联成删除
                                relate.DateModified = DateTime.Now;
                                relate.Chre_Source = "188";//20170612需求新增子来源、指定代码字段
                                relate.Appoint_Code = o.nc_code;
                                qr_QrCodeRelateService.Instance.Update_Entity(relate);
                                #endregion
                                issucess=true;
                            }else
                            {
                                issucess=false;
                            }
                            #endregion

                            ComLib.LogLib.Log4NetBase.Log("3、====NC二维码修改：：：NCCode：：" + o.nc_code + "::==sceneid==::" + relate.qr_Scene_ID);
                        }
                    }
                    #endregion
                }
                #endregion
            }
            else if (o.state == "离职员工")
            {
                #region 离职员工的处理

                var relate = qr_QrCodeRelateService.Instance.Get_SingleEntity_byWhere(
                    qr_QrCodeRelate._.qr_Type == 1 && qr_QrCodeRelate._.IsDelete == false
                    && qr_QrCodeRelate._.rel_Code == o.nc_code);

                if (relate != null)
                {
                    var qrcode = qr_QrCodeService.Instance.Get_SingleEntity_byWhere(qr_QrCode._.id == relate.qr_CodeID);
                    if (qrcode != null)
                    {
                        #region 更新二维码，将二维码改成弃用

                        #region 编辑图文信息
                        var extra = new List<extra>() { 
                                    new extra { 
                                        title = "马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇", 
                                        shortdesc = "一键注册，终身礼遇", 
                                        linkurl = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?openId=####", 
                                        picurl = "http://admin.mjn.howjoin.com/images/p1.jpg" 
                                    },
                                    new extra{
                                        title = "不可错过的海量专业孕育课程＆胎教早教资源！", 
                                        shortdesc = "有声资源每周更新，满足妈妈＆宝宝共同需求", 
                                        linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=2$sn=0d2328393e16964b17e11e30bb85378f$scene=1$srcid=06209qW7BXxURv3bBLvzMGHZ", 
                                        picurl = "http://admin.mjn.howjoin.com/images/p2.jpg" 
                                    },
                                    new extra{
                                        title = "【会员福利】奶粉、京东优惠券、巧虎礼包、滴滴红包等你拿,还有……", 
                                        shortdesc = "惊喜连连，准备好了么？", 
                                        linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=3$sn=8478a978359403d68c1452f132455d32$scene=1$srcid=0620AZL0kIEOrjKTGX6Y2emW", 
                                        picurl = "http://admin.mjn.howjoin.com/images/p3.jpg" 
                                    }
                                };
                        var message = new message2()
                        {
                            id = qrcode.id.ToString(),
                            title = o.name,
                            remark = o.name + "的旧二维码",
                            sacode = o.name + "的旧二维码",
                            extra = extra
                        };
                        #endregion

                        #region 请求VIGO更新接口
                        var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
                        var result = vigo.UpdateQrcode(message);
                        #endregion

                        if (result.IndexOf("成功") > 0)
                        {
                            #region 修改的启用二维码状态为弃用
                            qrcode.isdelete = true;
                            qrcode.DateModified = DateTime.Now;
                            qrcode.wx_title = "";
                            qrcode.wx_content = "";
                            qrcode.wx_url = "";
                            qrcode.wx_imgurl = "";
                            issucess = qr_QrCodeService.Instance.Update_Entity(qrcode);
                            #endregion

                            #region 修改关联成删除
                            relate.IsDelete = true;
                            relate.DateModified = DateTime.Now;                          
                            relate.Chre_Source = "188";//20170612需求新增子来源、指定代码字段
                            relate.Appoint_Code = o.nc_code;
                            qr_QrCodeRelateService.Instance.Update_Entity(relate);
                            #endregion
                        }
                        else
                        {
                            ComLib.LogLib.Log4NetBase.Log("3、====NC离职，弃用二维码失败：：：NCCode：：" + o.nc_code + "::==message==::" + result);
                        }
                        #endregion
                        ComLib.LogLib.Log4NetBase.Log("3、====NC离职，将此二维码弃用：：：NCCode：：" + o.nc_code + "::==sceneid==::" + relate.qr_Scene_ID);
                    }
                }
                else
                {
                    //查不到代表已经离职过，也是代表操作成功
                    issucess = true;
                }

                #endregion
            }
            else
            {
                ComLib.LogLib.Log4NetBase.Log("更新一个二维码错误::会员状态异常::新的状态:" + o.state);
            }
            return issucess;
        }


        /// <summary>
        /// NC妈妈班二维码
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public bool SyncMM(Entity.nc_ncuser o)
        {
            bool issucess = false;
            if (o.state == "活跃员工" || o.state == "休眠员工")
            {
                #region 活跃员工的处理 qr_Type=1006

                var has = qr_QrCodeRelateService.Instance.Get_IsExist_ByWhere(
                    qr_QrCodeRelate._.qr_Type == 1006 && qr_QrCodeRelate._.IsDelete == false
                    && qr_QrCodeRelate._.rel_Code == o.nc_code);

                if (!has)
                {
                    #region 如果在关联表没有NC对应的记录，就新增二维码

                    //查询是否有以前弃用的二维码
                    var oldqr = qr_QrCodeService.Instance.Get_Entitys_ByTop(1, qr_QrCode._.type == 1006 &&
                        qr_QrCode._.isdelete == true, qr_QrCode._.scene_id.Asc);

                    if (oldqr.IsNullOrCountLTE0())
                    {
                        #region 使用新的

                        #region get sceneid
                        var sceneid = 48458;//测试先用活动的场景值
                        qr_QrCodeType type = qr_QrCodeTypeService.Instance.Get_SingleEntity_byWhere(qr_QrCodeType._.QrCodeTypeID == 1006);
                        sceneid = type.QT_CurrentIndex.ConvertTo<int>() + 1;
                        #endregion

                        #region 编辑one图文信息

                        #region 编辑单图文信息
                        var extra = new List<extra>() { 
                                        new extra { 
                                        title = "【专家学堂】与专家对话,做A＋妈妈", 
                                        shortdesc = "听了这些资深专家的话,你也能成为A＋妈妈哟~还不快来参与?",                               
                                        picurl = "http://admin.mjn.howjoin.com/images/epcblue.jpg",
                                        linkurl = "https://epclass.meadjohnson.joypaw.com/?channel=3$ncid="+o.nc_code+"$openId=####", 
                                     }
                             };

                        var message = new message()
                        {
                            title = o.name,
                            remark = o.name + "的二维码",
                            sacode = "test",
                            scene_id = sceneid,
                            extra = extra
                        };
                        #endregion

                        #region 请求vigo 生成二维码
                        var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
                        var resultentity = vigo.CreateQrcode(message);

                        while (resultentity.errcode == "12")
                        {
                            ComLib.LogLib.Log4NetBase.Log("二维码sceneid重复" + sceneid);
                            sceneid -= 1;
                            message.scene_id = sceneid;
                            resultentity = vigo.CreateQrcode(message);
                        }
                        while (resultentity.errcode == "02")
                        {
                            ComLib.LogLib.Log4NetBase.Log(resultentity.errmsg + sceneid);
                            sceneid -= 1;
                            message.scene_id = sceneid;
                            resultentity = vigo.CreateQrcode(message);
                        }
                        #endregion

                        #region 将返回的信息保存
                        if (resultentity.errcode == "00")
                        {
                            #region 保存二维码
                            qr_QrCode qrcode = new qr_QrCode()
                            {
                                id = resultentity.message.id,
                                type = 1006,
                                scene_id = resultentity.message.scene_id,
                                sacode = resultentity.message.sacode,
                                title = resultentity.message.title,
                                remark = resultentity.message.remark,
                                code_url = resultentity.message.code_url,
                                qrcode_url = resultentity.message.qrcode_url,
                                addtime = resultentity.message.addtime,
                                status = resultentity.message.status,
                                DateModified = DateTime.Now,
                                wx_title = "【美赞臣】微信注册",
                                wx_content = "注册成为会员，享受更多优惠折扣！",
                                wx_url = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode=" + o.nc_code + "$openId=####",
                                wx_imgurl = "http://admin.mjn.howjoin.com/images/logo_b.png",
                                isdelete = false
                            };
                            issucess = qr_QrCodeService.Instance.Add_Entity(qrcode);
                            #endregion

                            #region 添加关联
                            qr_QrCodeRelate qrrelate = new qr_QrCodeRelate()
                            {
                                qr_Type = 1006,//1006是NC妈妈班
                                qr_CodeID = resultentity.message.id,
                                qr_Scene_ID = resultentity.message.scene_id,
                                rel_Code = o.nc_code,
                                IsDelete = false,
                                CreateOn = DateTime.Now,
                                Chre_Source = "",//20170612需求新增子来源、指定代码字段
                                Appoint_Code = o.nc_code
                            };
                            qr_QrCodeRelateService.Instance.Add_Entity(qrrelate);
                            #endregion

                            #region 更改二维码类型表
                            type.QT_CurrentIndex = sceneid;
                            type.QT_Used = type.QT_Used + 1;
                            type.QT_Surplus = type.QT_Surplus - 1;
                            qr_QrCodeTypeService.Instance.Update_Entity(type);
                            #endregion
                        }
                        #endregion

                        ComLib.LogLib.Log4NetBase.Log("3、====新增单图文NCMM二维码========新增===NCCode:" + o.nc_code + "::==sceneid==" + resultentity.message.scene_id);
                        #endregion

                        #endregion
                    }
                    else
                    {
                        #region 使用弃用的

                        var qr = oldqr[0];

                        #region 更新二维码

                        #region 编辑图文信息
                        var extra = new List<extra>() { 
                                        new extra { 
                                        title = "【专家学堂】与专家对话,做A＋妈妈", 
                                        shortdesc = "听了这些资深专家的话,你也能成为A＋妈妈哟~还不快来参与?",                               
                                        picurl = "http://admin.mjn.howjoin.com/images/epcblue.jpg",
                                        linkurl = "https://epclass.meadjohnson.joypaw.com/?channel=3$ncid="+o.nc_code+"$openId=####", 
                                     }
                             };
                        var message = new message2()
                        {
                            id = qr.id.ToString(),
                            title = o.name,
                            remark = o.name + "的二维码",
                            sacode = o.name + "的二维码",
                            extra = extra
                        };
                        #endregion

                        var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
                        var result = vigo.UpdateQrcode(message);

                        #region 修改弃用的二维码状态为启用
                        qr.title = o.name;
                        qr.remark = o.name + "的二维码";
                        qr.isdelete = false;
                        qr.DateModified = DateTime.Now;
                        qr.wx_title = "【美赞臣】微信注册";
                        qr.wx_content = "注册成为会员，享受更多优惠折扣！";
                        qr.wx_url = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode=" + o.nc_code + "$openId=####";
                        qr.wx_imgurl = "http://admin.mjn.howjoin.com/images/logo_b.png";
                        qr_QrCodeService.Instance.Update_Entity(qr);
                        #endregion

                        #endregion

                        #region 新建关联
                        qr_QrCodeRelate qrrelate = new qr_QrCodeRelate()
                        {
                            qr_Type = 1006,//1006是NC妈妈班
                            qr_CodeID = qr.id,
                            qr_Scene_ID = qr.scene_id,
                            rel_Code = o.nc_code,
                            IsDelete = false,//启用
                            CreateOn = DateTime.Now,
                            Chre_Source = "",//20170612需求新增子来源、指定代码字段
                            Appoint_Code = o.nc_code
                        };
                        issucess = qr_QrCodeRelateService.Instance.Add_Entity(qrrelate);
                        #endregion

                        ComLib.LogLib.Log4NetBase.Log("3、====NCMM为活跃，使用旧的二维码：：：NCCode：：" + o.nc_code + "::==sceneid==::" + qrrelate.qr_Scene_ID);

                        #endregion
                    }
                    #endregion
                }
                else
                {
                    #region 如果有了二维码，也更新一次
                    var relate = qr_QrCodeRelateService.Instance.Get_SingleEntity_byWhere(
                    qr_QrCodeRelate._.qr_Type == 1006 && qr_QrCodeRelate._.IsDelete == false
                    && qr_QrCodeRelate._.rel_Code == o.nc_code);

                    if (relate != null)
                    {
                        var qrcode = qr_QrCodeService.Instance.Get_SingleEntity_byWhere(qr_QrCode._.id == relate.qr_CodeID);
                        if (qrcode != null)
                        {
                            #region 更新二维码

                            #region 编辑图文信息
                            var extra = new List<extra>() { 
                                        new extra { 
                                        title = "【专家学堂】与专家对话,做A＋妈妈",
                                        shortdesc = "听了这些资深专家的话,你也能成为A＋妈妈哟~还不快来参与?",                               
                                        picurl = "http://admin.mjn.howjoin.com/images/epcblue.jpg",
                                        linkurl = "https://epclass.meadjohnson.joypaw.com/?channel=3$ncid="+o.nc_code+"$openId=####", 
                                     }
                             };
                            var message = new message2()
                            {
                                id = qrcode.id.ToString(),
                                title = o.name,
                                remark = o.name + "的二维码",
                                sacode = o.name + "的二维码",
                                extra = extra
                            };
                            #endregion

                            var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
                            var result = vigo.UpdateQrcode(message);
                            if (result.IndexOf("成功") > 0)
                            {
                                #region 将此NCMM更新子来源、指定代码
                                relate.Chre_Source = "";//20170612需求新增子来源、指定代码字段
                                relate.Appoint_Code = o.nc_code;
                                relate.DateModified = DateTime.Now;
                                qr_QrCodeRelateService.Instance.Update_Entity(relate);
                                #endregion
                                issucess = true;
                            }
                            else
                            {
                                issucess = false;
                            }
                            #endregion

                            ComLib.LogLib.Log4NetBase.Log("3、====NCMM二维码修改：：：NCCode：：" + o.nc_code + "::==sceneid==::" + relate.qr_Scene_ID);
                        }
                    }
                    #endregion
                }
                #endregion
            }
            else if (o.state == "离职员工")
            {
                #region 离职员工的处理

                var relate = qr_QrCodeRelateService.Instance.Get_SingleEntity_byWhere(
                    qr_QrCodeRelate._.qr_Type == 1006 && qr_QrCodeRelate._.IsDelete == false
                    && qr_QrCodeRelate._.rel_Code == o.nc_code);

                if (relate != null)
                {
                    var qrcode = qr_QrCodeService.Instance.Get_SingleEntity_byWhere(qr_QrCode._.id == relate.qr_CodeID);
                    if (qrcode != null)
                    {
                        #region 更新二维码，将二维码改成弃用

                        #region 编辑图文信息
                        var extra = new List<extra>() { 
                                        new extra { 
                                        title = "【专家学堂】与专家对话,做A＋妈妈", 
                                        shortdesc = "听了这些资深专家的话,你也能成为A＋妈妈哟~还不快来参与?",                               
                                        picurl = "http://admin.mjn.howjoin.com/images/epcblue.jpg",
                                        linkurl = "https://epclass.meadjohnson.joypaw.com/?openId=####",
                                     }
                             };
                        var message = new message2()
                        {
                            id = qrcode.id.ToString(),
                            title = o.name,
                            remark = o.name + "的旧二维码",
                            sacode = o.name + "的旧二维码",
                            extra = extra
                        };
                        #endregion

                        #region 请求VIGO更新接口
                        var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
                        var result = vigo.UpdateQrcode(message);
                        #endregion

                        if (result.IndexOf("成功") > 0)
                        {
                            #region 修改的启用二维码状态为弃用
                            qrcode.isdelete = true;//弃用
                            qrcode.DateModified = DateTime.Now;
                            qrcode.wx_title = "";
                            qrcode.wx_content = "";
                            qrcode.wx_url = "";
                            qrcode.wx_imgurl = "";
                            issucess = qr_QrCodeService.Instance.Update_Entity(qrcode);
                            #endregion

                            #region 修改关联成删除
                            relate.IsDelete = true;
                            relate.DateModified = DateTime.Now;                           
                            relate.Chre_Source = "";//20170612需求新增子来源、指定代码字段
                            relate.Appoint_Code = o.nc_code;
                            qr_QrCodeRelateService.Instance.Update_Entity(relate);
                            #endregion
                        }
                        else
                        {
                            ComLib.LogLib.Log4NetBase.Log("3、====NCMM离职，弃用二维码失败：：：NCCode：：" + o.nc_code + "::==message==::" + result);
                        }
                        #endregion
                        ComLib.LogLib.Log4NetBase.Log("3、====NCMM离职，将此二维码弃用：：：NCCode：：" + o.nc_code + "::==sceneid==::" + relate.qr_Scene_ID);
                    }
                }
                else
                {
                    //查不到代表已经离职过，也是代表操作成功
                    issucess = true;
                }

                #endregion
            }
            else
            {
                ComLib.LogLib.Log4NetBase.Log("更新一个二维码错误::会员状态异常::新的状态:" + o.state);
            }
            return issucess;
        }

        #region 测试NC
        //测试NC新增二维码
//        public string Sync_QrCodeAdd(string actiontype, string NCcode)
//        {

//            string msg = "";
//            if (NCcode != "")
//            {
//                #region 指定NC信息添加
//                //var nclist = nc_ncuserService.Instance.Get_Entity_byWhere(Entity.nc_ncuser._.isneedupdate == true, null);
//                var nclist = nc_ncuserService.Instance.Get_Entity_byWhere(Entity.nc_ncuser._.nc_code == NCcode, null);
//                if (nclist == null)
//                {
//                    msg = "NCcode不存在，请更换！";
//                }
//                else
//                {
//                    ComLib.LogLib.Log4NetBase.Log("3、==========================更新NC二维码===========数量:" + nclist.Count);
//                    nclist.ForEach(o =>
//                    {
//                        try
//                        {
//                            if (o.state == "活跃员工" || o.state == "休眠员工")
//                            {
//                                #region 活跃员工的处理

//                                var has = qr_QrCodeRelateService.Instance.Get_IsExist_ByWhere(
//                                    qr_QrCodeRelate._.qr_Type == 1 && qr_QrCodeRelate._.IsDelete == false
//                                    && qr_QrCodeRelate._.rel_Code == o.nc_code);

//                                if (!has)
//                                {
//                                    #region 如果在关联表没有NC对应的记录，就新增二维码

//                                    //查询是否有以前弃用的二维码
//                                    var oldqr = qr_QrCodeService.Instance.Get_Entitys_ByTop(1, qr_QrCode._.type == 1 &&
//                                        qr_QrCode._.isdelete == true, qr_QrCode._.scene_id.Asc);

//                                    if (oldqr.IsNullOrCountLTE0())
//                                    {
//                                        #region 使用新的

//                                        #region get sceneid
//                                        var sceneid = 0;
//                                        qr_QrCodeType type = qr_QrCodeTypeService.Instance.Get_SingleEntity_byWhere(qr_QrCodeType._.QrCodeTypeID == 1);
//                                        sceneid = type.QT_CurrentIndex.ConvertTo<int>() + 1;
//                                        #endregion

//                                        if (actiontype == "one")
//                                        {
//                                            #region 编辑one图文信息

//                                            #region 编辑单图文信息
//                                            var extra = new List<extra>() { 
//                                                  new extra { 
//                                                    title = "马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇", 
//                                                    shortdesc = "一键注册，终身礼遇",                                
//                                                    picurl = "http://admin.mjn.howjoin.com/images/p1.jpg",
//                                                    linkurl = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode=20000150$openId=####" 
//                                                      }
//                                            };
                                            
//                                            var message = new message()
//                                            {
//                                                title = o.name,
//                                                remark = o.name + "的二维码",
//                                                sacode = "test",
//                                                scene_id = sceneid,
//                                                extra = extra
//                                            };
//                                            #endregion


//                                            #region 请求vigo 生成二维码
//                                            var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
//                                            var resultentity = vigo.CreateQrcode(message);
//                                            #endregion

//                                            while (resultentity.errcode == "12")
//                                            {
//                                                ComLib.LogLib.Log4NetBase.Log("二维码sceneid重复" + sceneid);
//                                                sceneid -= 1;
//                                                message.scene_id = sceneid;
//                                                resultentity = vigo.CreateQrcode(message);
//                                            }
//                                            while (resultentity.errcode == "02")
//                                            {
//                                                ComLib.LogLib.Log4NetBase.Log(resultentity.errmsg + sceneid);
//                                                sceneid -= 1;
//                                                message.scene_id = sceneid;
//                                                resultentity = vigo.CreateQrcode(message);
//                                            }
//                                            #region 将返回的信息保存
//                                            if (resultentity.errcode == "00")
//                                            {
//                                                #region 保存二维码
//                                                qr_QrCode qrcode = new qr_QrCode()
//                                                {
//                                                    id = resultentity.message.id,
//                                                    type = 1,
//                                                    scene_id = resultentity.message.scene_id,
//                                                    sacode = resultentity.message.sacode,
//                                                    title = resultentity.message.title,
//                                                    remark = resultentity.message.remark,
//                                                    code_url = resultentity.message.code_url,
//                                                    qrcode_url = resultentity.message.qrcode_url,
//                                                    addtime = resultentity.message.addtime,
//                                                    status = resultentity.message.status,
//                                                    DateModified = DateTime.Now,
//                                                    wx_title = "【美赞臣】微信注册",
//                                                    wx_content = "注册成为会员，享受更多优惠折扣！",
//                                                    wx_url = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode=" + o.nc_code + "$openId=####",
//                                                    wx_imgurl = "http://admin.mjn.howjoin.com/images/logo_b.png",
//                                                    isdelete = false
//                                                };
//                                                qr_QrCodeService.Instance.Add_Entity(qrcode);
//                                                #endregion

//                                                #region 添加关联
//                                                qr_QrCodeRelate qrrelate = new qr_QrCodeRelate()
//                                                {
//                                                    qr_Type = 1,//1是NC
//                                                    qr_CodeID = resultentity.message.id,
//                                                    qr_Scene_ID = resultentity.message.scene_id,
//                                                    rel_Code = o.nc_code,
//                                                    IsDelete = false,
//                                                    CreateOn = DateTime.Now
//                                                };
//                                                qr_QrCodeRelateService.Instance.Add_Entity(qrrelate);
//                                                #endregion

//                                                #region 更改二维码类型表
//                                                type.QT_CurrentIndex = sceneid;
//                                                type.QT_Used = type.QT_Used + 1;
//                                                type.QT_Surplus = type.QT_Surplus - 1;
//                                                qr_QrCodeTypeService.Instance.Update_Entity(type);
//                                                #endregion

//                                                msg = "请求成功,二维码：" + resultentity.message.qrcode_url + "code_url:" + resultentity.message.code_url + "" + resultentity.message.id;
//                                            }
//                                            else
//                                            {
//                                                msg = "请求失败" + resultentity.errmsg;
//                                            }

//                                            #endregion

//                                            ComLib.LogLib.Log4NetBase.Log("3、====新增单图文NC二维码========新增===NCCode:" + o.nc_code + "::==sceneid==" + resultentity.message.scene_id);
//                                            #endregion
//                                        }
//                                        else 
//                                        {
//                                            #region 编辑more图文信息
//                                            var extra = new List<extra>() { 
//                                            new extra { 
//                                                title = "马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇", 
//                                                shortdesc = "一键注册，终身礼遇", 
//                                                linkurl = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode="+o.nc_code+"$openId=####", 
//                                                picurl = "http://admin.mjn.howjoin.com/images/p1.jpg" 
//                                            },
//                                            new extra{
//                                                title = "不可错过的海量专业孕育课程＆胎教早教资源！", 
//                                                shortdesc = "有声资源每周更新，满足妈妈＆宝宝共同需求", 
//                                                linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=2$sn=0d2328393e16964b17e11e30bb85378f$scene=1$srcid=06209qW7BXxURv3bBLvzMGHZ", 
//                                                picurl = "http://admin.mjn.howjoin.com/images/p2.jpg" 
//                                            },
//                                            new extra{
//                                                title = "【会员福利】奶粉、京东优惠券、巧虎礼包、滴滴红包等你拿,还有……", 
//                                                shortdesc = "惊喜连连，准备好了么？", 
//                                                linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=3$sn=8478a978359403d68c1452f132455d32$scene=1$srcid=0620AZL0kIEOrjKTGX6Y2emW", 
//                                                picurl = "http://admin.mjn.howjoin.com/images/p3.jpg" 
//                                            }
//                                         };

//                                            var message = new message()
//                                            {
//                                                title = o.name,
//                                                remark = o.name + "的二维码",
//                                                sacode = "指定NC编号test",
//                                                scene_id = sceneid,
//                                                extra = extra
//                                            };
//                                            #endregion

//                                            #region 请求vigo 生成二维码
//                                            var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
//                                            var resultentity = vigo.CreateQrcode(message);
//                                            #endregion

//                                            while (resultentity.errcode == "12")
//                                            {
//                                                ComLib.LogLib.Log4NetBase.Log("二维码sceneid重复" + sceneid);
//                                                sceneid += 1;
//                                                message.scene_id = sceneid;
//                                                resultentity = vigo.CreateQrcode(message);
//                                            }

//                                            #region 将返回的信息保存
//                                            if (resultentity.errcode == "00")
//                                            {
//                                                #region 保存二维码
//                                                qr_QrCode qrcode = new qr_QrCode()
//                                                {
//                                                    id = resultentity.message.id,
//                                                    type = 1,
//                                                    scene_id = resultentity.message.scene_id,
//                                                    sacode = resultentity.message.sacode,
//                                                    title = resultentity.message.title,
//                                                    remark = resultentity.message.remark,
//                                                    code_url = resultentity.message.code_url,
//                                                    qrcode_url = resultentity.message.qrcode_url,
//                                                    addtime = resultentity.message.addtime,
//                                                    status = resultentity.message.status,
//                                                    DateModified = DateTime.Now,
//                                                    wx_title = "【美赞臣】微信注册",
//                                                    wx_content = "注册成为会员，享受更多优惠折扣！",
//                                                    wx_url = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode=" + o.nc_code + "$openId=####",
//                                                    wx_imgurl = "http://admin.mjn.howjoin.com/images/logo_b.png",
//                                                    isdelete = false
//                                                };
//                                                qr_QrCodeService.Instance.Add_Entity(qrcode);
//                                                #endregion

//                                                #region 添加关联
//                                                qr_QrCodeRelate qrrelate = new qr_QrCodeRelate()
//                                                {
//                                                    qr_Type = 1,//1是NC
//                                                    qr_CodeID = resultentity.message.id,
//                                                    qr_Scene_ID = resultentity.message.scene_id,
//                                                    rel_Code = o.nc_code,
//                                                    IsDelete = false,
//                                                    CreateOn = DateTime.Now
//                                                };
//                                                qr_QrCodeRelateService.Instance.Add_Entity(qrrelate);
//                                                #endregion

//                                                #region 更改二维码类型表
//                                                type.QT_CurrentIndex = sceneid;
//                                                type.QT_Used = type.QT_Used + 1;
//                                                type.QT_Surplus = type.QT_Surplus - 1;
//                                                qr_QrCodeTypeService.Instance.Update_Entity(type);
//                                                #endregion
//                                            }
//                                            #endregion

//                                            ComLib.LogLib.Log4NetBase.Log("3、====新增NC二维码========新增===NCCode:" + o.nc_code + "::==sceneid==" + resultentity.message.scene_id);
//                                        }
//                                        #endregion                  
//                                    }
//                                    else
//                                    {
//                                        #region 使用弃用的

//                                        var qr = oldqr[0];

//                                        #region 更新二维码

//                                        #region 编辑图文信息
//                                        var extra = new List<extra>() { 
//                                            new extra { 
//                                                title = "马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇", 
//                                                shortdesc = "一键注册，终身礼遇", 
//                                                linkurl = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode="+o.nc_code+"$openId=####", 
//                                                picurl = "http://admin.mjn.howjoin.com/images/p1.jpg" 
//                                            },
//                                            new extra{
//                                                title = "不可错过的海量专业孕育课程＆胎教早教资源！", 
//                                                shortdesc = "有声资源每周更新，满足妈妈＆宝宝共同需求", 
//                                                linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=2$sn=0d2328393e16964b17e11e30bb85378f$scene=1$srcid=06209qW7BXxURv3bBLvzMGHZ", 
//                                                picurl = "http://admin.mjn.howjoin.com/images/p2.jpg" 
//                                            },
//                                            new extra{
//                                                title = "【会员福利】奶粉、京东优惠券、巧虎礼包、滴滴红包等你拿,还有……", 
//                                                shortdesc = "惊喜连连，准备好了么？", 
//                                                linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=3$sn=8478a978359403d68c1452f132455d32$scene=1$srcid=0620AZL0kIEOrjKTGX6Y2emW", 
//                                                picurl = "http://admin.mjn.howjoin.com/images/p3.jpg" 
//                                            }
//                                        };
//                                        var message = new message2()
//                                        {
//                                            id = qr.id.ToString(),
//                                            title = o.name,
//                                            remark = o.name + "的二维码",
//                                            sacode = o.name + "的二维码",
//                                            extra = extra
//                                        };
//                                        #endregion

//                                        var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
//                                        var result = vigo.UpdateQrcode(message);

//                                        #region 修改弃用的二维码状态为启用
//                                        qr.title = o.name;
//                                        qr.remark = o.name + "的二维码";
//                                        qr.isdelete = false;
//                                        qr.DateModified = DateTime.Now;
//                                        qr.wx_title = "【美赞臣】微信注册";
//                                        qr.wx_content = "注册成为会员，享受更多优惠折扣！";
//                                        qr.wx_url = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode=" + o.nc_code + "$openId=####";
//                                        qr.wx_imgurl = "http://admin.mjn.howjoin.com/images/logo_b.png";
//                                        qr_QrCodeService.Instance.Update_Entity(qr);
//                                        #endregion

//                                        #endregion

//                                        #region 新建关联
//                                        qr_QrCodeRelate qrrelate = new qr_QrCodeRelate()
//                                        {
//                                            qr_Type = 1,//1是NC
//                                            qr_CodeID = qr.id,
//                                            qr_Scene_ID = qr.scene_id,
//                                            rel_Code = o.nc_code,
//                                            IsDelete = false,//启用
//                                            CreateOn = DateTime.Now
//                                        };
//                                        qr_QrCodeRelateService.Instance.Add_Entity(qrrelate);
//                                        #endregion

//                                        ComLib.LogLib.Log4NetBase.Log("3、====NC为活跃，使用旧的二维码：：：NCCode：：" + o.nc_code + "::==sceneid==::" + qrrelate.qr_Scene_ID);

//                                        #endregion
//                                    }
//                                    #endregion
//                                }
//                                else
//                                {
//                                    #region 如果有了二维码，也更新一次
//                                    var relate = qr_QrCodeRelateService.Instance.Get_SingleEntity_byWhere(
//                                    qr_QrCodeRelate._.qr_Type == 1 && qr_QrCodeRelate._.IsDelete == false
//                                    && qr_QrCodeRelate._.rel_Code == o.nc_code);

//                                    if (relate != null)
//                                    {
//                                        var qrcode = qr_QrCodeService.Instance.Get_SingleEntity_byWhere(qr_QrCode._.id == relate.qr_CodeID);
//                                        if (qrcode != null)
//                                        {
//                                            #region 更新二维码

//                                            #region 编辑图文信息
//                                            var extra = new List<extra>() { 
//                                                new extra { 
//                                                    title = "马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇", 
//                                                    shortdesc = "一键注册，终身礼遇", 
//                                                    linkurl = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode="+o.nc_code+"$openId=####", 
//                                                    picurl = "http://admin.mjn.howjoin.com/images/p1.jpg" 
//                                                },
//                                                new extra{
//                                                    title = "不可错过的海量专业孕育课程＆胎教早教资源！", 
//                                                    shortdesc = "有声资源每周更新，满足妈妈＆宝宝共同需求", 
//                                                    linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=2$sn=0d2328393e16964b17e11e30bb85378f$scene=1$srcid=06209qW7BXxURv3bBLvzMGHZ", 
//                                                    picurl = "http://admin.mjn.howjoin.com/images/p2.jpg" 
//                                                },
//                                                new extra{
//                                                    title = "【会员福利】奶粉、京东优惠券、巧虎礼包、滴滴红包等你拿,还有……", 
//                                                    shortdesc = "惊喜连连，准备好了么？", 
//                                                    linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=3$sn=8478a978359403d68c1452f132455d32$scene=1$srcid=0620AZL0kIEOrjKTGX6Y2emW", 
//                                                    picurl = "http://admin.mjn.howjoin.com/images/p3.jpg" 
//                                                }
//                                            };
//                                            var message = new message2()
//                                            {
//                                                id = qrcode.id.ToString(),
//                                                title = o.name,
//                                                remark = o.name + "的二维码",
//                                                sacode = o.name + "的二维码",
//                                                extra = extra
//                                            };
//                                            #endregion

//                                            var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
//                                            var result = vigo.UpdateQrcode(message);
//                                            #endregion

//                                            ComLib.LogLib.Log4NetBase.Log("3、====NC二维码修改：：：NCCode：：" + o.nc_code + "::==sceneid==::" + relate.qr_Scene_ID);
//                                        }
//                                    }
//                                    #endregion
//                                }
//                                #endregion
//                            }
//                            else
//                            {
//                                #region 离职员工的处理

//                                var relate = qr_QrCodeRelateService.Instance.Get_SingleEntity_byWhere(
//                                    qr_QrCodeRelate._.qr_Type == 1 && qr_QrCodeRelate._.IsDelete == false
//                                    && qr_QrCodeRelate._.rel_Code == o.nc_code);

//                                if (relate != null)
//                                {
//                                    var qrcode = qr_QrCodeService.Instance.Get_SingleEntity_byWhere(qr_QrCode._.id == relate.qr_CodeID);
//                                    if (qrcode != null)
//                                    {
//                                        #region 更新二维码，将二维码改成弃用

//                                        #region 编辑图文信息
//                                        var extra = new List<extra>() { 
//                                            new extra { 
//                                                title = "马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇", 
//                                                shortdesc = "一键注册，终身礼遇", 
//                                                linkurl = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?openId=####", 
//                                                picurl = "http://admin.mjn.howjoin.com/images/p1.jpg" 
//                                            },
//                                            new extra{
//                                                title = "不可错过的海量专业孕育课程＆胎教早教资源！", 
//                                                shortdesc = "有声资源每周更新，满足妈妈＆宝宝共同需求", 
//                                                linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=2$sn=0d2328393e16964b17e11e30bb85378f$scene=1$srcid=06209qW7BXxURv3bBLvzMGHZ", 
//                                                picurl = "http://admin.mjn.howjoin.com/images/p2.jpg" 
//                                            },
//                                            new extra{
//                                                title = "【会员福利】奶粉、京东优惠券、巧虎礼包、滴滴红包等你拿,还有……", 
//                                                shortdesc = "惊喜连连，准备好了么？", 
//                                                linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=3$sn=8478a978359403d68c1452f132455d32$scene=1$srcid=0620AZL0kIEOrjKTGX6Y2emW", 
//                                                picurl = "http://admin.mjn.howjoin.com/images/p3.jpg" 
//                                            }
//                                        };
//                                        var message = new message2()
//                                        {
//                                            id = qrcode.id.ToString(),
//                                            title = o.name,
//                                            remark = o.name + "的旧二维码",
//                                            sacode = o.name + "的旧二维码",
//                                            extra = extra
//                                        };
//                                        #endregion

//                                        var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
//                                        var result = vigo.UpdateQrcode(message);

//                                        #region 修改的启用二维码状态为弃用
//                                        qrcode.isdelete = true;//弃用
//                                        qrcode.DateModified = DateTime.Now;
//                                        qrcode.wx_title = "";
//                                        qrcode.wx_content = "";
//                                        qrcode.wx_url = "";
//                                        qrcode.wx_imgurl = "";
//                                        qr_QrCodeService.Instance.Update_Entity(qrcode);
//                                        #endregion

//                                        #endregion

//                                        #region 修改关联成删除
//                                        relate.IsDelete = true;
//                                        relate.DateModified = DateTime.Now;
//                                        qr_QrCodeRelateService.Instance.Update_Entity(relate);
//                                        #endregion

//                                        ComLib.LogLib.Log4NetBase.Log("3、====NC离职，将此二维码弃用：：：NCCode：：" + o.nc_code + "::==sceneid==::" + relate.qr_Scene_ID);
//                                    }
//                                }

//                                #endregion
//                            }

//                            #region 将nc置为已修改
//                            o.isneedupdate = false;
//                            nc_ncuserService.Instance.Update_Entity(o);
//                            #endregion
//                        }
//                        catch (Exception ex)
//                        {
//                            ComLib.LogLib.Log4NetBase.Log("更新一个二维码错误::::" + ex.Message + ex.StackTrace);
//                        }
//                    });

//                }
//                #endregion
//            }
//            else
//            {
//                #region 自定义NCcode（需要修改数据库指定NC的 isneedupdate == 1）
//                var nclist = nc_ncuserService.Instance.Get_Entity_byWhere(Entity.nc_ncuser._.isneedupdate == true, null);
//                ComLib.LogLib.Log4NetBase.Log("3、==========================创建NC二维码===========数量:" + nclist.Count);
//                nclist.ForEach(o =>
//                {
//                    try
//                    {
//                        #region 测试新增NC
//                        if (o.state == "活跃员工" || o.state == "休眠员工")
//                        {
//                            #region 活跃员工的处理 测试创建NC

//                            //var has = qr_QrCodeRelateService.Instance.Get_IsExist_ByWhere(
//                            //    qr_QrCodeRelate._.qr_Type == 1 && qr_QrCodeRelate._.IsDelete == false
//                            //    && qr_QrCodeRelate._.rel_Code == o.nc_code);
//                            bool has = false;
//                            if (has == false)
//                            {
//                                #region 如果在关联表没有NC对应的记录，就新增二维码

//                                //查询是否有以前弃用的二维码
//                                //var oldqr = qr_QrCodeService.Instance.Get_Entitys_ByTop(1, qr_QrCode._.type == 1 &&
//                                //    qr_QrCode._.isdelete == true, qr_QrCode._.scene_id.Asc);
//                                //var oldqr = null;
//                                bool isold = false;
//                                if (isold == false)
//                                {
//                                    #region 使用新的

//                                    #region get sceneid
//                                    var sceneid = 99912;
//                                    qr_QrCodeType type = qr_QrCodeTypeService.Instance.Get_SingleEntity_byWhere(qr_QrCodeType._.QrCodeTypeID == 1);
//                                    //sceneid = type.QT_CurrentIndex.ConvertTo<int>() + 1;
//                                    int sceneid2 = Convert.ToInt32(sceneid);
//                                    #endregion

//                                    if (actiontype == "one")
//                                    {
//                                        #region one
//                                        #region 编辑单图文信息
//                                        var extra = new List<extra>() { 
//                                  new extra { 
//                                    title = "马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇", 
//                                    shortdesc = "一键注册，终身礼遇",                                
//                                    picurl = "http://admin.mjn.howjoin.com/images/p1.jpg",
//                                    linkurl = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode=20000150$openId=####", 
//                                      }
//                                    };
//                                        //var extra = "[{\"title\":\"" + "一键注册，终身礼遇"
//                                        //              + "\",\"shortdesc\":\"" + "马上加入美赞臣A+妈妈会，尊享360°孕育礼遇"20000149
//                                        //              + "\",\"picurl\":\"" + "http://admin.mjn.howjoin.com/images/p1.jpg"
//                                        //              + "\",\"linkurl\":\"" + "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode=20000149$openId=####"
//                                        //              + "\"}]";
//                                        // {"extra":[{"linkurl":"http:\/\/crmweb.meadjohnson.com.cn\/mjnwx\/client_phone.action?regSubChannel=188$NCCode=20000149$openId=####","picurl":"http:\/\/admin.mjn.howjoin.com\/images\/p1.jpg","shortdesc":"一键注册，终身礼遇","title":"马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇"}],"title":"TestQrCodeOne","remark":"Test","sacode":"","scene_id":99900}
//                                        //var message = "{\"title\":\"" + "TestQrCodeOne"
//                                        //               + "\",\"remark\":\"" + "Test"
//                                        //               + "\",\"sacode\":\"" + ""
//                                        //               + "\",\"scene_id\":" + "99900"
//                                        //               + ",\"extra\":" + extra
//                                        //               + "}";

//                                        var message = new message()
//                                        {
//                                            //title = o.name,
//                                            //remark = o.name + "的二维码",
//                                            //sacode = "test",
//                                            //scene_id = sceneid.ToString(),
//                                            //extra = extra
//                                            title = "TestQrCodeOne",
//                                            remark = "Test",
//                                            sacode = "",
//                                            scene_id = sceneid,
//                                            extra = extra
//                                        };
//                                        #endregion


//                                        #region 请求vigo 生成二维码
//                                        var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
//                                        var resultentity = vigo.CreateQrcode(message);
//                                        #endregion

//                                        while (resultentity.errcode == "12")
//                                        {
//                                            ComLib.LogLib.Log4NetBase.Log("二维码sceneid重复" + sceneid);
//                                            sceneid -= 1;
//                                            message.scene_id = sceneid;
//                                            resultentity = vigo.CreateQrcode(message);
//                                        }
//                                        while (resultentity.errcode == "02")
//                                        {
//                                            ComLib.LogLib.Log4NetBase.Log(resultentity.errmsg + sceneid);
//                                            sceneid -= 1;
//                                            message.scene_id = sceneid;
//                                            resultentity = vigo.CreateQrcode(message);
//                                        }
//                                        #region 将返回的信息保存
//                                        if (resultentity.errcode == "00")
//                                        {
//                                            #region 保存二维码
//                                            qr_QrCode qrcode = new qr_QrCode()
//                                            {
//                                                id = resultentity.message.id,
//                                                type = 1,
//                                                scene_id = resultentity.message.scene_id,
//                                                sacode = resultentity.message.sacode,
//                                                title = resultentity.message.title,
//                                                remark = resultentity.message.remark,
//                                                code_url = resultentity.message.code_url,
//                                                qrcode_url = resultentity.message.qrcode_url,
//                                                addtime = resultentity.message.addtime,
//                                                status = resultentity.message.status,
//                                                DateModified = DateTime.Now,
//                                                wx_title = "【美赞臣】微信注册",
//                                                wx_content = "注册成为会员，享受更多优惠折扣！",
//                                                wx_url = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode=" + o.nc_code + "$openId=####",
//                                                wx_imgurl = "http://admin.mjn.howjoin.com/images/logo_b.png",
//                                                isdelete = false
//                                            };
//                                            qr_QrCodeService.Instance.Add_Entity(qrcode);
//                                            #endregion

//                                            #region 添加关联
//                                            qr_QrCodeRelate qrrelate = new qr_QrCodeRelate()
//                                            {
//                                                qr_Type = 1,//1是NC
//                                                qr_CodeID = resultentity.message.id,
//                                                qr_Scene_ID = resultentity.message.scene_id,
//                                                rel_Code = o.nc_code,
//                                                IsDelete = false,
//                                                CreateOn = DateTime.Now
//                                            };
//                                            qr_QrCodeRelateService.Instance.Add_Entity(qrrelate);
//                                            #endregion

//                                            #region 更改二维码类型表
//                                            type.QT_CurrentIndex = sceneid;
//                                            type.QT_Used = type.QT_Used + 1;
//                                            type.QT_Surplus = type.QT_Surplus - 1;
//                                            qr_QrCodeTypeService.Instance.Update_Entity(type);
//                                            #endregion

//                                            msg = "请求成功,二维码：" + resultentity.message.qrcode_url + "code_url:" + resultentity.message.code_url + "" + resultentity.message.id;
//                                        }
//                                        else
//                                        {
//                                            msg = "请求失败" + resultentity.errmsg;
//                                        }

//                                        #endregion

//                                        ComLib.LogLib.Log4NetBase.Log("3、====新增单图文NC二维码========新增===NCCode:" + o.nc_code + "::==sceneid==" + resultentity.message.scene_id);
//                                        #endregion
//                                    }
//                                    else
//                                    {
//                                        #region more
//                                        #region 编辑多图文信息
//                                        var extra = new List<extra>() { 
//                                new extra { 
//                                    title = "马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇", 
//                                    shortdesc = "一键注册，终身礼遇", 
//                                    linkurl = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode="+o.nc_code+"$openId=####", 
//                                    picurl = "http://admin.mjn.howjoin.com/images/p1.jpg" 
//                                },
//                                new extra{
//                                    title = "不可错过的海量专业孕育课程＆胎教早教资源！", 
//                                    shortdesc = "有声资源每周更新，满足妈妈＆宝宝共同需求", 
//                                    linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=2$sn=0d2328393e16964b17e11e30bb85378f$scene=1$srcid=06209qW7BXxURv3bBLvzMGHZ", 
//                                    picurl = "http://admin.mjn.howjoin.com/images/p2.jpg" 
//                                },
//                                new extra{
//                                    title = "【会员福利】奶粉、京东优惠券、巧虎礼包、滴滴红包等你拿,还有……", 
//                                    shortdesc = "惊喜连连，准备好了么？", 
//                                    linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=3$sn=8478a978359403d68c1452f132455d32$scene=1$srcid=0620AZL0kIEOrjKTGX6Y2emW", 
//                                    picurl = "http://admin.mjn.howjoin.com/images/p3.jpg" 
//                                }
//                            };
//                                        var message = new message()
//                                        {
//                                            title = "TestQrCodeOne",
//                                            remark = "Test",
//                                            sacode = "",
//                                            scene_id = sceneid,
//                                            extra = extra
//                                        };
//                                        #endregion

//                                        #region 请求vigo 生成二维码
//                                        var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
//                                        var resultentity = vigo.CreateQrcode(message);
//                                        #endregion

//                                        while (resultentity.errcode == "12")
//                                        {
//                                            ComLib.LogLib.Log4NetBase.Log("二维码sceneid重复" + sceneid);
//                                            sceneid -= 1;
//                                            message.scene_id = sceneid;
//                                            resultentity = vigo.CreateQrcode(message);
//                                        }

//                                        #region 将返回的信息保存
//                                        if (resultentity.errcode == "00")
//                                        {
//                                            #region 保存二维码
//                                            qr_QrCode qrcode = new qr_QrCode()
//                                            {
//                                                id = resultentity.message.id,
//                                                type = 1,
//                                                scene_id = resultentity.message.scene_id,
//                                                sacode = resultentity.message.sacode,
//                                                title = resultentity.message.title,
//                                                remark = resultentity.message.remark,
//                                                code_url = resultentity.message.code_url,
//                                                qrcode_url = resultentity.message.qrcode_url,
//                                                addtime = resultentity.message.addtime,
//                                                status = resultentity.message.status,
//                                                DateModified = DateTime.Now,
//                                                wx_title = "【美赞臣】微信注册",
//                                                wx_content = "注册成为会员，享受更多优惠折扣！",
//                                                wx_url = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode=" + o.nc_code + "$openId=####",
//                                                wx_imgurl = "http://admin.mjn.howjoin.com/images/logo_b.png",
//                                                isdelete = false
//                                            };
//                                            qr_QrCodeService.Instance.Add_Entity(qrcode);
//                                            #endregion

//                                            #region 添加关联
//                                            qr_QrCodeRelate qrrelate = new qr_QrCodeRelate()
//                                            {
//                                                qr_Type = 1,//1是NC
//                                                qr_CodeID = resultentity.message.id,
//                                                qr_Scene_ID = resultentity.message.scene_id,
//                                                rel_Code = o.nc_code,
//                                                IsDelete = false,
//                                                CreateOn = DateTime.Now
//                                            };
//                                            qr_QrCodeRelateService.Instance.Add_Entity(qrrelate);
//                                            #endregion

//                                            #region 更改二维码类型表
//                                            type.QT_CurrentIndex = sceneid;
//                                            type.QT_Used = type.QT_Used + 1;
//                                            type.QT_Surplus = type.QT_Surplus - 1;
//                                            qr_QrCodeTypeService.Instance.Update_Entity(type);
//                                            #endregion

//                                            msg = "请求成功,二维码：" + resultentity.message.qrcode_url + "/code_url:" + resultentity.message.code_url + "" + resultentity.message.id;
//                                        }
//                                        else
//                                        {
//                                            msg = "请求失败" + resultentity.errmsg;
//                                        }

//                                        #endregion

//                                        ComLib.LogLib.Log4NetBase.Log("3、====新增单图文NC二维码========新增===NCCode:" + o.nc_code + "::==sceneid==" + resultentity.message.scene_id);
//                                        #endregion
//                                    }
//                                    #endregion
//                                }
//                                else
//                                {

//                                }
//                                #endregion
//                            }
//                            else
//                            {
//                                #region 如果有了二维码，也更新一次
//                                var relate = qr_QrCodeRelateService.Instance.Get_SingleEntity_byWhere(
//                                qr_QrCodeRelate._.qr_Type == 1 && qr_QrCodeRelate._.IsDelete == false
//                                && qr_QrCodeRelate._.rel_Code == o.nc_code);

//                                if (relate != null)
//                                {
//                                    var qrcode = qr_QrCodeService.Instance.Get_SingleEntity_byWhere(qr_QrCode._.id == relate.qr_CodeID);
//                                    if (qrcode != null)
//                                    {
//                                        #region 更新二维码

//                                        #region 编辑图文信息
//                                        var extra = new List<extra>() { 
//                                        new extra { 
//                                            title = "马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇", 
//                                            shortdesc = "一键注册，终身礼遇", 
//                                            linkurl = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode="+o.nc_code+"$openId=####", 
//                                            picurl = "http://admin.mjn.howjoin.com/images/p1.jpg" 
//                                        },
//                                        new extra{
//                                            title = "不可错过的海量专业孕育课程＆胎教早教资源！", 
//                                            shortdesc = "有声资源每周更新，满足妈妈＆宝宝共同需求", 
//                                            linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=2$sn=0d2328393e16964b17e11e30bb85378f$scene=1$srcid=06209qW7BXxURv3bBLvzMGHZ", 
//                                            picurl = "http://admin.mjn.howjoin.com/images/p2.jpg" 
//                                        },
//                                        new extra{
//                                            title = "【会员福利】奶粉、京东优惠券、巧虎礼包、滴滴红包等你拿,还有……", 
//                                            shortdesc = "惊喜连连，准备好了么？", 
//                                            linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=3$sn=8478a978359403d68c1452f132455d32$scene=1$srcid=0620AZL0kIEOrjKTGX6Y2emW", 
//                                            picurl = "http://admin.mjn.howjoin.com/images/p3.jpg" 
//                                        }
//                                    };
//                                        var message = new message2()
//                                        {
//                                            id = qrcode.id.ToString(),
//                                            title = o.name,
//                                            remark = o.name + "的二维码",
//                                            sacode = o.name + "的二维码",
//                                            extra = extra
//                                        };
//                                        #endregion

//                                        var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
//                                        var result = vigo.UpdateQrcode(message);
//                                        //if (result.errcode == "00")
//                                        msg = "修改成功" + result.ToString();
//                                        #endregion

//                                        ComLib.LogLib.Log4NetBase.Log("3、====NC二维码修改：：：NCCode：：" + o.nc_code + "::==sceneid==::" + relate.qr_Scene_ID);
//                                    }
//                                }
//                                #endregion
//                            }
//                            #endregion

//                        }
//                        else
//                        {

//                        }

//                        #region 将nc置为已修改
//                        o.isneedupdate = false;
//                        nc_ncuserService.Instance.Update_Entity(o);
//                        #endregion
//                        #endregion


//                    }
//                    catch (Exception ex)
//                    {
//                        ComLib.LogLib.Log4NetBase.Log("更新一个二维码错误::::" + ex.Message + ex.StackTrace);
//                    }
//                });
//                #endregion

//            }

//            return msg;
//        }
//        //测试NC更新二维码
//        public string  Sync_QrCode( string nccode)
//        {

//            string msg = "";
//            #region 先得到所有的需要更新二维码的NC   905
//            var nclist = nc_ncuserService.Instance.Get_Entity_byWhere(Entity.nc_ncuser._.isneedupdate == true, null);
//            ComLib.LogLib.Log4NetBase.Log("3、==========================创建NC二维码===========数量:" + nclist.Count);
//            nclist.ForEach(o =>
//            {
//                try
//                {
//                    #region 测试新增NC
//                    //if (o.state == "活跃员工" || o.state == "休眠员工")
//                    //{
//                    //    #region 活跃员工的处理 测试创建NC

//                    //    //var has = qr_QrCodeRelateService.Instance.Get_IsExist_ByWhere(
//                    //    //    qr_QrCodeRelate._.qr_Type == 1 && qr_QrCodeRelate._.IsDelete == false
//                    //    //    && qr_QrCodeRelate._.rel_Code == o.nc_code);
//                    //    //bool has = false;
//                    //    //if (has == false)
//                    //    //{
//                    //    //    #region 如果在关联表没有NC对应的记录，就新增二维码

//                    //    //    //查询是否有以前弃用的二维码
//                    //    //    //var oldqr = qr_QrCodeService.Instance.Get_Entitys_ByTop(1, qr_QrCode._.type == 1 &&
//                    //    //    //    qr_QrCode._.isdelete == true, qr_QrCode._.scene_id.Asc);
//                    //    //    //var oldqr = null;
//                    //    //    bool isold = false;
//                    //    //    if (isold == false)
//                    //    //    {
//                    //    //        #region 使用新的

//                    //    //        #region get sceneid
//                    //    //        var sceneid = 0;
//                    //    //        qr_QrCodeType type = qr_QrCodeTypeService.Instance.Get_SingleEntity_byWhere(qr_QrCodeType._.QrCodeTypeID == 1);
//                    //    //        sceneid = type.QT_CurrentIndex.ConvertTo<int>() + 1;
//                    //    //        #endregion

//                    //    //        #region 编辑图文信息
//                    //    //        List<extra> extra = new List<extra>() { 
//                    //    //        new extra { 
//                    //    //            title = "标题", 
//                    //    //            shortdesc = "图文简介",                                
//                    //    //            picurl = "http://img15.3lian.com/2016/w4/48/69.jpg",
//                    //    //             linkurl = "https://www.baidu.com/", 
//                    //    //        }
//                    //    //    };
        
//                    //    //        var message = new message()
//                    //    //        {
//                    //    //            title ="TestQrCodeOne",                                   
//                    //    //            remark = "Test",
//                    //    //            sacode = "",
//                    //    //            scene_id = "9993",
//                    //    //            extra = extra                                  
//                    //    //        };
//                    //    //        #endregion

//                    //    //        #region 请求vigo 生成二维码
//                    //    //        var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
//                    //    //        var resultentity = vigo.CreateQrcode(message);
//                    //    //        #endregion

//                    //    //        while (resultentity.errcode == "12")
//                    //    //        {
//                    //    //            ComLib.LogLib.Log4NetBase.Log("二维码sceneid重复" + sceneid);
//                    //    //            sceneid += 1;
//                    //    //            message.scene_id = sceneid.ToString();
//                    //    //            resultentity = vigo.CreateQrcode(message);
//                    //    //        }

//                    //    //        #region 将返回的信息保存
//                    //    //        if (resultentity.errcode == "00")
//                    //    //        {
//                    //    //            #region 保存二维码
//                    //    //            //qr_QrCode qrcode = new qr_QrCode()
//                    //    //            //{
//                    //    //            //    id = resultentity.message.id,
//                    //    //            //    type = 1,
//                    //    //            //    scene_id = resultentity.message.scene_id,
//                    //    //            //    sacode = resultentity.message.sacode,
//                    //    //            //    title = resultentity.message.title,
//                    //    //            //    remark = resultentity.message.remark,
//                    //    //            //    code_url = resultentity.message.code_url,
//                    //    //            //    qrcode_url = resultentity.message.qrcode_url,
//                    //    //            //    addtime = resultentity.message.addtime,
//                    //    //            //    status = resultentity.message.status,
//                    //    //            //    DateModified = DateTime.Now,
//                    //    //            //    wx_title = "【美赞臣】微信注册",
//                    //    //            //    wx_content = "注册成为会员，享受更多优惠折扣！",
//                    //    //            //    wx_url = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode=" + o.nc_code + "$openId=####",
//                    //    //            //    wx_imgurl = "http://admin.mjn.howjoin.com/images/logo_b.png",
//                    //    //            //    isdelete = false
//                    //    //            //};
//                    //    //            //qr_QrCodeService.Instance.Add_Entity(qrcode);
//                    //    //            #endregion
                                   
//                    //    //            #region 添加关联
//                    //    //            //qr_QrCodeRelate qrrelate = new qr_QrCodeRelate()
//                    //    //            //{
//                    //    //            //    qr_Type = 1,//1是NC
//                    //    //            //    qr_CodeID = resultentity.message.id,
//                    //    //            //    qr_Scene_ID = resultentity.message.scene_id,
//                    //    //            //    rel_Code = o.nc_code,
//                    //    //            //    IsDelete = false,
//                    //    //            //    CreateOn = DateTime.Now
//                    //    //            //};
//                    //    //            //qr_QrCodeRelateService.Instance.Add_Entity(qrrelate);
//                    //    //            #endregion

//                    //    //            #region 更改二维码类型表
//                    //    //            //type.QT_CurrentIndex = sceneid;
//                    //    //            //type.QT_Used = type.QT_Used + 1;
//                    //    //            //type.QT_Surplus = type.QT_Surplus - 1;
//                    //    //            //qr_QrCodeTypeService.Instance.Update_Entity(type);
//                    //    //            #endregion

//                    //    //            msg = "请求成功,二维码："+resultentity.message.qrcode_url;
//                    //    //        }
//                    //    //        else {
//                    //    //            msg = "请求失败";
//                    //    //        }

//                    //    //        #endregion

//                    //    //        ComLib.LogLib.Log4NetBase.Log("3、====新增NC二维码========新增===NCCode:" + o.nc_code + "::==sceneid==" + resultentity.message.scene_id);

//                    //    //        #endregion
//                    //    //    }
//                    //    //    else
//                    //    //    {
                               
//                    //    //    }
//                    //    //    #endregion
//                    //    //}
//                    //    //else
//                    //    //{
//                    //    //    #region 如果有了二维码，也更新一次
//                    //    //    var relate = qr_QrCodeRelateService.Instance.Get_SingleEntity_byWhere(
//                    //    //    qr_QrCodeRelate._.qr_Type == 1 && qr_QrCodeRelate._.IsDelete == false
//                    //    //    && qr_QrCodeRelate._.rel_Code == o.nc_code);

//                    //    //    if (relate != null)
//                    //    //    {
//                    //    //        var qrcode = qr_QrCodeService.Instance.Get_SingleEntity_byWhere(qr_QrCode._.id == relate.qr_CodeID);
//                    //    //        if (qrcode != null)
//                    //    //        {
//                    //    //            #region 更新二维码

//                    //    //            #region 编辑图文信息
//                    //    //            var extra = new List<extra>() { 
//                    //    //                new extra { 
//                    //    //                    title = "马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇", 
//                    //    //                    shortdesc = "一键注册，终身礼遇", 
//                    //    //                    linkurl = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode="+o.nc_code+"$openId=####", 
//                    //    //                    picurl = "http://admin.mjn.howjoin.com/images/p1.jpg" 
//                    //    //                },
//                    //    //                new extra{
//                    //    //                    title = "不可错过的海量专业孕育课程＆胎教早教资源！", 
//                    //    //                    shortdesc = "有声资源每周更新，满足妈妈＆宝宝共同需求", 
//                    //    //                    linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=2$sn=0d2328393e16964b17e11e30bb85378f$scene=1$srcid=06209qW7BXxURv3bBLvzMGHZ", 
//                    //    //                    picurl = "http://admin.mjn.howjoin.com/images/p2.jpg" 
//                    //    //                },
//                    //    //                new extra{
//                    //    //                    title = "【会员福利】奶粉、京东优惠券、巧虎礼包、滴滴红包等你拿,还有……", 
//                    //    //                    shortdesc = "惊喜连连，准备好了么？", 
//                    //    //                    linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=3$sn=8478a978359403d68c1452f132455d32$scene=1$srcid=0620AZL0kIEOrjKTGX6Y2emW", 
//                    //    //                    picurl = "http://admin.mjn.howjoin.com/images/p3.jpg" 
//                    //    //                }
//                    //    //            };
//                    //    //            var message = new message2()
//                    //    //            {
//                    //    //                id = qrcode.id.ToString(),
//                    //    //                title = o.name,
//                    //    //                remark = o.name + "的二维码",
//                    //    //                sacode = o.name + "的二维码",
//                    //    //                extra = extra
//                    //    //            };
//                    //    //            #endregion

//                    //    //            var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
//                    //    //            var result = vigo.UpdateQrcode(message);
//                    //    //            //if (result.errcode == "00")
//                    //    //            msg = "修改成功";
//                    //    //            #endregion

//                    //    //            ComLib.LogLib.Log4NetBase.Log("3、====NC二维码修改：：：NCCode：：" + o.nc_code + "::==sceneid==::" + relate.qr_Scene_ID);
//                    //    //        }
//                    //    //    }
//                    //    //    #endregion
//                    //    //}
//                    //    #endregion

//                    //}
//                    //else
//                    //{
                        
//                    //}

//                    //#region 将nc置为已修改
//                    //o.isneedupdate = false;
//                    //nc_ncuserService.Instance.Update_Entity(o);
//                    //#endregion
//                    #endregion

//                    #region 更新二维码
//                    var entity = nc_ncuserService.Instance.Get_SingleEntity_byWhere(ShareMarking.Entity.nc_ncuser._.nc_code == "905");
//                    if (entity != null)
//                    {
//                        var relate = qr_QrCodeRelateService.Instance.Get_SingleEntity_byWhere(
//                            qr_QrCodeRelate._.qr_Type == 1 && qr_QrCodeRelate._.IsDelete == false
//                            && qr_QrCodeRelate._.rel_Code == entity.nc_code);
//                        if (relate != null)
//                        {
//                            var qrcode = qr_QrCodeService.Instance.Get_SingleEntity_byWhere(qr_QrCode._.id == relate.qr_CodeID);//"15968"
//                            if (qrcode != null)
//                            {
//                                #region 更新二维码

//                                #region 编辑图文信息
//                                //title = "马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇", 
//                               // title = "不可错过的海量专业孕育课程＆胎教早教资源！", 
//                                var extra = new List<extra>() { 
//                                        new extra { 
//                                            //title = "2016-12-01测试更新A", 
//                                            title = "马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇"+DateTime.Now,
//                                            shortdesc = "一键注册，终身礼遇", 
//                                            linkurl = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode="+o.nc_code+"$openId=####", 
//                                            picurl = "http://admin.mjn.howjoin.com/images/p1.jpg" 
//                                        },
//                                        new extra{
//                                            //title = "2016-12-01测试更新B", 
//                                            title = "不可错过的海量专业孕育课程＆胎教早教资源！"+DateTime.Now, 
//                                            shortdesc = "有声资源每周更新，满足妈妈＆宝宝共同需求", 
//                                            linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=2$sn=0d2328393e16964b17e11e30bb85378f$scene=1$srcid=06209qW7BXxURv3bBLvzMGHZ", 
//                                            picurl = "http://admin.mjn.howjoin.com/images/p2.jpg" 
//                                        },
//                                        new extra{
//                                            title = "【会员福利】奶粉、京东优惠券、巧虎礼包、滴滴红包等你拿,还有……"+DateTime.Now, 
//                                            //title = "2016-12-01测试更新C",
//                                            shortdesc = "惊喜连连，准备好了么？", 
//                                            linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=3$sn=8478a978359403d68c1452f132455d32$scene=1$srcid=0620AZL0kIEOrjKTGX6Y2emW", 
//                                            picurl = "http://admin.mjn.howjoin.com/images/p3.jpg" 
//                                        }
//                                };
//                                var message = new message2()
//                                {
//                                    //id = qrcode.id.ToString(),
//                                    //title = entity.name,
//                                    //remark = entity.name + "多图文",
//                                    //sacode = entity.name + "多图文",
//                                    //extra = extra
//                                    id = qrcode.id.ToString(),
//                                    title = "TestQrCodeMore",
//                                    remark = "Test",
//                                    sacode = "测试更新多图文",
//                                    extra = extra
//                                };
//                                #endregion

//                                var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
//                                var info= vigo.UpdateQrcode(message);
//                                msg = info;
//                                #endregion

//                            }
//                        }
//                    }
//                    #endregion                            

//                    #region 正式
//                    //if (o.state == "活跃员工" || o.state == "休眠员工")
//                    //{
//                    //    #region 活跃员工的处理

//                    //    var has = qr_QrCodeRelateService.Instance.Get_IsExist_ByWhere(
//                    //        qr_QrCodeRelate._.qr_Type == 1 && qr_QrCodeRelate._.IsDelete == false
//                    //        && qr_QrCodeRelate._.rel_Code == o.nc_code);

//                    //    if (!has)
//                    //    {
//                    //        #region 如果在关联表没有NC对应的记录，就新增二维码

//                    //        //查询是否有以前弃用的二维码
//                    //        var oldqr = qr_QrCodeService.Instance.Get_Entitys_ByTop(1, qr_QrCode._.type == 1 &&
//                    //            qr_QrCode._.isdelete == true, qr_QrCode._.scene_id.Asc);

//                    //        if (oldqr.IsNullOrCountLTE0())
//                    //        {
//                    //            #region 使用新的

//                    //            #region get sceneid
//                    //            var sceneid = 0;
//                    //            qr_QrCodeType type = qr_QrCodeTypeService.Instance.Get_SingleEntity_byWhere(qr_QrCodeType._.QrCodeTypeID == 1);
//                    //            sceneid = type.QT_CurrentIndex.ConvertTo<int>() + 1;
//                    //            #endregion

//                    //            #region 编辑图文信息
//                    //            var extra = new List<extra>() { 
//                    //            new extra { 
//                    //                title = "马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇", 
//                    //                shortdesc = "一键注册，终身礼遇", 
//                    //                linkurl = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode="+o.nc_code+"$openId=####", 
//                    //                picurl = "http://admin.mjn.howjoin.com/images/p1.jpg" 
//                    //            },
//                    //            new extra{
//                    //                title = "不可错过的海量专业孕育课程＆胎教早教资源！", 
//                    //                shortdesc = "有声资源每周更新，满足妈妈＆宝宝共同需求", 
//                    //                linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=2$sn=0d2328393e16964b17e11e30bb85378f$scene=1$srcid=06209qW7BXxURv3bBLvzMGHZ", 
//                    //                picurl = "http://admin.mjn.howjoin.com/images/p2.jpg" 
//                    //            },
//                    //            new extra{
//                    //                title = "【会员福利】奶粉、京东优惠券、巧虎礼包、滴滴红包等你拿,还有……", 
//                    //                shortdesc = "惊喜连连，准备好了么？", 
//                    //                linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=3$sn=8478a978359403d68c1452f132455d32$scene=1$srcid=0620AZL0kIEOrjKTGX6Y2emW", 
//                    //                picurl = "http://admin.mjn.howjoin.com/images/p3.jpg" 
//                    //            }
//                    //        };
//                    //            var message = new message()
//                    //            {
//                    //                title = o.name,
//                    //                remark = o.name + "的二维码",
//                    //                sacode = "test",
//                    //                scene_id = sceneid,
//                    //                extra = extra
//                    //            };
//                    //            #endregion

//                    //            #region 请求vigo 生成二维码
//                    //            var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
//                    //            var resultentity = vigo.CreateQrcode(message);
//                    //            #endregion

//                    //            while (resultentity.errcode == "12")
//                    //            {
//                    //                ComLib.LogLib.Log4NetBase.Log("二维码sceneid重复" + sceneid);
//                    //                sceneid += 1;
//                    //                message.scene_id = sceneid;
//                    //                resultentity = vigo.CreateQrcode(message);
//                    //            }

//                    //            #region 将返回的信息保存
//                    //            if (resultentity.errcode == "00")
//                    //            {
//                    //                #region 保存二维码
//                    //                qr_QrCode qrcode = new qr_QrCode()
//                    //                {
//                    //                    id = resultentity.message.id,
//                    //                    type = 1,
//                    //                    scene_id = resultentity.message.scene_id,
//                    //                    sacode = resultentity.message.sacode,
//                    //                    title = resultentity.message.title,
//                    //                    remark = resultentity.message.remark,
//                    //                    code_url = resultentity.message.code_url,
//                    //                    qrcode_url = resultentity.message.qrcode_url,
//                    //                    addtime = resultentity.message.addtime,
//                    //                    status = resultentity.message.status,
//                    //                    DateModified = DateTime.Now,
//                    //                    wx_title = "【美赞臣】微信注册",
//                    //                    wx_content = "注册成为会员，享受更多优惠折扣！",
//                    //                    wx_url = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode=" + o.nc_code + "$openId=####",
//                    //                    wx_imgurl = "http://admin.mjn.howjoin.com/images/logo_b.png",
//                    //                    isdelete = false
//                    //                };
//                    //                qr_QrCodeService.Instance.Add_Entity(qrcode);
//                    //                #endregion

//                    //                #region 添加关联
//                    //                qr_QrCodeRelate qrrelate = new qr_QrCodeRelate()
//                    //                {
//                    //                    qr_Type = 1,//1是NC
//                    //                    qr_CodeID = resultentity.message.id,
//                    //                    qr_Scene_ID = resultentity.message.scene_id,
//                    //                    rel_Code = o.nc_code,
//                    //                    IsDelete = false,
//                    //                    CreateOn = DateTime.Now
//                    //                };
//                    //                qr_QrCodeRelateService.Instance.Add_Entity(qrrelate);
//                    //                #endregion

//                    //                #region 更改二维码类型表
//                    //                type.QT_CurrentIndex = sceneid;
//                    //                type.QT_Used = type.QT_Used + 1;
//                    //                type.QT_Surplus = type.QT_Surplus - 1;
//                    //                qr_QrCodeTypeService.Instance.Update_Entity(type);
//                    //                #endregion
//                    //            }
//                    //            #endregion

//                    //            ComLib.LogLib.Log4NetBase.Log("3、====新增NC二维码========新增===NCCode:" + o.nc_code + "::==sceneid==" + resultentity.message.scene_id);

//                    //            #endregion
//                    //        }
//                    //        else
//                    //        {
//                    //            #region 使用弃用的

//                    //            var qr = oldqr[0];

//                    //            #region 更新二维码

//                    //            #region 编辑图文信息
//                    //            var extra = new List<extra>() { 
//                    //                new extra { 
//                    //                    title = "马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇", 
//                    //                    shortdesc = "一键注册，终身礼遇", 
//                    //                    linkurl = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode="+o.nc_code+"$openId=####", 
//                    //                    picurl = "http://admin.mjn.howjoin.com/images/p1.jpg" 
//                    //                },
//                    //                new extra{
//                    //                    title = "不可错过的海量专业孕育课程＆胎教早教资源！", 
//                    //                    shortdesc = "有声资源每周更新，满足妈妈＆宝宝共同需求", 
//                    //                    linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=2$sn=0d2328393e16964b17e11e30bb85378f$scene=1$srcid=06209qW7BXxURv3bBLvzMGHZ", 
//                    //                    picurl = "http://admin.mjn.howjoin.com/images/p2.jpg" 
//                    //                },
//                    //                new extra{
//                    //                    title = "【会员福利】奶粉、京东优惠券、巧虎礼包、滴滴红包等你拿,还有……", 
//                    //                    shortdesc = "惊喜连连，准备好了么？", 
//                    //                    linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=3$sn=8478a978359403d68c1452f132455d32$scene=1$srcid=0620AZL0kIEOrjKTGX6Y2emW", 
//                    //                    picurl = "http://admin.mjn.howjoin.com/images/p3.jpg" 
//                    //                }
//                    //            };
//                    //            var message = new message2()
//                    //            {
//                    //                id = qr.id.ToString(),
//                    //                title = o.name,
//                    //                remark = o.name + "的二维码",
//                    //                sacode = o.name + "的二维码",
//                    //                extra = extra
//                    //            };
//                    //            #endregion

//                    //            var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
//                    //            var result = vigo.UpdateQrcode(message);

//                    //            #region 修改弃用的二维码状态为启用
//                    //            qr.title = o.name;
//                    //            qr.remark = o.name + "的二维码";
//                    //            qr.isdelete = false;
//                    //            qr.DateModified = DateTime.Now;
//                    //            qr.wx_title = "【美赞臣】微信注册";
//                    //            qr.wx_content = "注册成为会员，享受更多优惠折扣！";
//                    //            qr.wx_url = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode=" + o.nc_code + "$openId=####";
//                    //            qr.wx_imgurl = "http://admin.mjn.howjoin.com/images/logo_b.png";
//                    //            qr_QrCodeService.Instance.Update_Entity(qr);
//                    //            #endregion

//                    //            #endregion

//                    //            #region 新建关联
//                    //            qr_QrCodeRelate qrrelate = new qr_QrCodeRelate()
//                    //            {
//                    //                qr_Type = 1,//1是NC
//                    //                qr_CodeID = qr.id,
//                    //                qr_Scene_ID = qr.scene_id,
//                    //                rel_Code = o.nc_code,
//                    //                IsDelete = false,
//                    //                CreateOn = DateTime.Now
//                    //            };
//                    //            qr_QrCodeRelateService.Instance.Add_Entity(qrrelate);
//                    //            #endregion

//                    //            ComLib.LogLib.Log4NetBase.Log("3、====NC为活跃，使用旧的二维码：：：NCCode：：" + o.nc_code + "::==sceneid==::" + qrrelate.qr_Scene_ID);

//                    //            #endregion
//                    //        }
//                    //        #endregion
//                    //    }
//                    //    else
//                    //    {
//                    //        #region 如果有了二维码，也更新一次
//                    //        var relate = qr_QrCodeRelateService.Instance.Get_SingleEntity_byWhere(
//                    //        qr_QrCodeRelate._.qr_Type == 1 && qr_QrCodeRelate._.IsDelete == false
//                    //        && qr_QrCodeRelate._.rel_Code == o.nc_code);

//                    //        if (relate != null)
//                    //        {
//                    //            var qrcode = qr_QrCodeService.Instance.Get_SingleEntity_byWhere(qr_QrCode._.id == relate.qr_CodeID);
//                    //            if (qrcode != null)
//                    //            {
//                    //                #region 更新二维码

//                    //                #region 编辑图文信息
//                    //                var extra = new List<extra>() { 
//                    //                    //new extra { 
//                    //                    //    title = "马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇", 
//                    //                    //    shortdesc = "一键注册，终身礼遇", 
//                    //                    //    linkurl = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode="+o.nc_code+"$openId=####", 
//                    //                    //    picurl = "http://admin.mjn.howjoin.com/images/p1.jpg" 
//                    //                    //},
//                    //                    //new extra{
//                    //                    //    title = "不可错过的海量专业孕育课程＆胎教早教资源！", 
//                    //                    //    shortdesc = "有声资源每周更新，满足妈妈＆宝宝共同需求", 
//                    //                    //    linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=2$sn=0d2328393e16964b17e11e30bb85378f$scene=1$srcid=06209qW7BXxURv3bBLvzMGHZ", 
//                    //                    //    picurl = "http://admin.mjn.howjoin.com/images/p2.jpg" 
//                    //                    //},
//                    //                    //new extra{
//                    //                    //    title = "【会员福利】奶粉、京东优惠券、巧虎礼包、滴滴红包等你拿,还有……", 
//                    //                    //    shortdesc = "惊喜连连，准备好了么？", 
//                    //                    //    linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=3$sn=8478a978359403d68c1452f132455d32$scene=1$srcid=0620AZL0kIEOrjKTGX6Y2emW", 
//                    //                    //    picurl = "http://admin.mjn.howjoin.com/images/p3.jpg" 
//                    //                    //}
//                    //                };
//                    //                var message = new message2()
//                    //                {
//                    //                    //{"id":"15896","title":"TestQQQ","sacode":"","remark":"TESTWWW","extra":""}
//                    //                    //id = qrcode.id.ToString(),
//                    //                    //title = o.name,
//                    //                    //remark = o.name + "的二维码",
//                    //                    //sacode = o.name + "的二维码",
//                    //                    //extra = extra
//                    //                    id = "15896",
//                    //                    title = "TestQQQ",
//                    //                    sacode = "",
//                    //                    remark = "TESTWWW",                                      
//                    //                    extra = extra
//                    //                };
//                    //                #endregion

//                    //                var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
//                    //                var result = vigo.UpdateQrcode(message);
//                    //                msg = "更新二维码请求信息：" + result.ToString();
//                    //                #endregion

//                    //                ComLib.LogLib.Log4NetBase.Log("3、====NC二维码修改：：：NCCode：：" + o.nc_code + "::==sceneid==::" + relate.qr_Scene_ID);
//                    //            }
//                    //        }
//                    //        #endregion
//                    //    }
//                    //    #endregion
//                    //}
//                    //else
//                    //{
//                    //    #region 离职员工的处理

//                    //    var relate = qr_QrCodeRelateService.Instance.Get_SingleEntity_byWhere(
//                    //        qr_QrCodeRelate._.qr_Type == 1 && qr_QrCodeRelate._.IsDelete == false
//                    //        && qr_QrCodeRelate._.rel_Code == o.nc_code);

//                    //    if (relate != null)
//                    //    {
//                    //        var qrcode = qr_QrCodeService.Instance.Get_SingleEntity_byWhere(qr_QrCode._.id == relate.qr_CodeID);
//                    //        if (qrcode != null)
//                    //        {
//                    //            #region 更新二维码，将二维码改成弃用

//                    //            #region 编辑图文信息
//                    //            var extra = new List<extra>() { 
//                    //                new extra { 
//                    //                    title = "马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇", 
//                    //                    shortdesc = "一键注册，终身礼遇", 
//                    //                    linkurl = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?openId=####", 
//                    //                    picurl = "http://admin.mjn.howjoin.com/images/p1.jpg" 
//                    //                },
//                    //                new extra{
//                    //                    title = "不可错过的海量专业孕育课程＆胎教早教资源！", 
//                    //                    shortdesc = "有声资源每周更新，满足妈妈＆宝宝共同需求", 
//                    //                    linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=2$sn=0d2328393e16964b17e11e30bb85378f$scene=1$srcid=06209qW7BXxURv3bBLvzMGHZ", 
//                    //                    picurl = "http://admin.mjn.howjoin.com/images/p2.jpg" 
//                    //                },
//                    //                new extra{
//                    //                    title = "【会员福利】奶粉、京东优惠券、巧虎礼包、滴滴红包等你拿,还有……", 
//                    //                    shortdesc = "惊喜连连，准备好了么？", 
//                    //                    linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=3$sn=8478a978359403d68c1452f132455d32$scene=1$srcid=0620AZL0kIEOrjKTGX6Y2emW", 
//                    //                    picurl = "http://admin.mjn.howjoin.com/images/p3.jpg" 
//                    //                }
//                    //            };
//                    //            var message = new message2()
//                    //            {
//                    //                id = qrcode.id.ToString(),
//                    //                title = o.name,
//                    //                remark = o.name + "的旧二维码",
//                    //                sacode = o.name + "的旧二维码",
//                    //                extra = extra
//                    //            };
//                    //            #endregion

//                    //            var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
//                    //            var result = vigo.UpdateQrcode(message);

//                    //            #region 修改的启用二维码状态为弃用
//                    //            qrcode.isdelete = true;
//                    //            qrcode.DateModified = DateTime.Now;
//                    //            qrcode.wx_title = "";
//                    //            qrcode.wx_content = "";
//                    //            qrcode.wx_url = "";
//                    //            qrcode.wx_imgurl = "";
//                    //            qr_QrCodeService.Instance.Update_Entity(qrcode);
//                    //            #endregion

//                    //            #endregion

//                    //            #region 修改关联成删除
//                    //            relate.IsDelete = true;
//                    //            relate.DateModified = DateTime.Now;
//                    //            qr_QrCodeRelateService.Instance.Update_Entity(relate);
//                    //            #endregion

//                    //            ComLib.LogLib.Log4NetBase.Log("3、====NC离职，将此二维码弃用：：：NCCode：：" + o.nc_code + "::==sceneid==::" + relate.qr_Scene_ID);
//                    //        }
//                    //    }

//                    //    #endregion
//                    //}

//                    #region 将nc置为已修改
//                    //o.isneedupdate = false;
//                    //nc_ncuserService.Instance.Update_Entity(o);
//                    #endregion
//                    #endregion
//                }
//                catch (Exception ex)
//                {
//                    ComLib.LogLib.Log4NetBase.Log("更新一个二维码错误::::" + ex.Message + ex.StackTrace);
//                }
//            });
//#endregion
//            return msg;
//        }
        #endregion

        #region 测试NC妈妈班 【qr_Type == 1006】
        //测试NC妈妈班新增二维码
        public string Sync_QrCodeAdd(string actiontype, string NCcode)
        {

            string msg = "";
            if (NCcode != "")
            {
                #region 指定NC信息添加
                var nclist = nc_ncuserService.Instance.Get_Entity_byWhere(Entity.nc_ncuser._.isneedupdate == true, null);
                //var nclist = nc_ncuserService.Instance.Get_Entity_byWhere(Entity.nc_ncuser._.nc_code == NCcode, null);
                if (nclist == null)
                {
                    msg = "NCcode不存在，请更换！";
                }
                else
                {
                    ComLib.LogLib.Log4NetBase.Log("3、==========================更新NC二维码===========数量:" + nclist.Count);
                    int sumnc = 0;
                    nclist.ForEach(o =>
                    {
                        try
                        {
                            if (o.state == "活跃员工" || o.state == "休眠员工")
                            {
                                #region 活跃员工的处理 1006

                                var has = qr_QrCodeRelateService.Instance.Get_IsExist_ByWhere(
                                    qr_QrCodeRelate._.qr_Type == 1006 && qr_QrCodeRelate._.IsDelete == false
                                    && qr_QrCodeRelate._.rel_Code == o.nc_code);

                                if (!has)
                                {
                                    #region 如果在关联表没有NC对应的记录，就新增二维码

                                    //查询是否有以前弃用的二维码
                                    var oldqr = qr_QrCodeService.Instance.Get_Entitys_ByTop(1, qr_QrCode._.type == 1006 &&
                                        qr_QrCode._.isdelete == true, qr_QrCode._.scene_id.Asc);

                                    if (oldqr.IsNullOrCountLTE0())
                                    {
                                        #region 使用新的

                                        #region get sceneid
                                        var sceneid = 48458;//测试先用活动的场景值
                                        qr_QrCodeType type = qr_QrCodeTypeService.Instance.Get_SingleEntity_byWhere(qr_QrCodeType._.QrCodeTypeID ==1006);
                                        sceneid = type.QT_CurrentIndex.ConvertTo<int>() + 1;
                                        #endregion

                                        if (actiontype == "one")
                                        {
                                            sumnc += 1;
                                            #region 编辑one图文信息
                                   
                                            //title = "EPC测试NC姓名:"+o.name+",NCCode:"+o.nc_code+"", 
                                            //        shortdesc = "EPC测试"+sumnc+"",                               
                                            //        picurl = "http://admin.mjn.howjoin.com/images/p1.jpg",
                                            //        linkurl = "https://tepclass.meadjohnson.joypaw.com/?channel=3&ncid="+o.nc_code+"$openId=####"


                                            //https://tepclass.meadjohnson.joypaw.com/?channel=3&ncid="+NCcode+"$openId=####", 
                                            //linkurl = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode="+NCcode+"$openId=####", 
                                            #region 编辑单图文信息
                                            var extra = new List<extra>() { 
                                                  new extra { 
                                                            title = "EPC测试NC姓名"+o.name+"NCCode"+o.nc_code+"", 
                                                            shortdesc = "EPC测试"+sumnc+"",                               
                                                            picurl = "http://admin.mjn.howjoin.com/images/p1.jpg",
                                                            linkurl = "https://tepclass.meadjohnson.joypaw.com/?channel=3$ncid="+o.nc_code+"$openId=####", 
                                                      }
                                            };
                                            
                                            var message = new message()
                                            {
                                                title = o.name,
                                                remark = o.name + "的二维码",
                                                sacode = "test",
                                                scene_id = sceneid,
                                                extra = extra
                                            };
                                            #endregion


                                            #region 请求vigo 生成二维码
                                            var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
                                            var resultentity = vigo.CreateQrcode(message);
                                            #endregion

                                            while (resultentity.errcode == "12")
                                            {
                                                ComLib.LogLib.Log4NetBase.Log("二维码sceneid重复" + sceneid);
                                                sceneid -= 1;
                                                message.scene_id = sceneid;
                                                resultentity = vigo.CreateQrcode(message);
                                            }
                                            while (resultentity.errcode == "02")
                                            {
                                                ComLib.LogLib.Log4NetBase.Log(resultentity.errmsg + sceneid);
                                                sceneid -= 1;
                                                message.scene_id = sceneid;
                                                resultentity = vigo.CreateQrcode(message);
                                            }
                                            #region 将返回的信息保存
                                            if (resultentity.errcode == "00")
                                            {
                                                #region 保存二维码
                                                qr_QrCode qrcode = new qr_QrCode()
                                                {
                                                    id = resultentity.message.id,
                                                    type = 1006,
                                                    scene_id = resultentity.message.scene_id,
                                                    sacode = resultentity.message.sacode,
                                                    title = resultentity.message.title,
                                                    remark = resultentity.message.remark,
                                                    code_url = resultentity.message.code_url,
                                                    qrcode_url = resultentity.message.qrcode_url,
                                                    addtime = resultentity.message.addtime,
                                                    status = resultentity.message.status,
                                                    DateModified = DateTime.Now,
                                                    wx_title = "【美赞臣】微信注册",
                                                    wx_content = "注册成为会员，享受更多优惠折扣！",
                                                    wx_url = "https://tepclass.meadjohnson.joypaw.com/?channel=3$ncid="+o.nc_code+"$openId=####",
                                                    wx_imgurl = "http://admin.mjn.howjoin.com/images/logo_b.png",
                                                    isdelete = false
                                                };
                                                qr_QrCodeService.Instance.Add_Entity(qrcode);
                                                #endregion

                                                #region 添加关联
                                                qr_QrCodeRelate qrrelate = new qr_QrCodeRelate()
                                                {
                                                    qr_Type = 1006,//1006是NC妈妈班
                                                    qr_CodeID = resultentity.message.id,
                                                    qr_Scene_ID = resultentity.message.scene_id,
                                                    rel_Code = o.nc_code,
                                                    IsDelete = false,
                                                    CreateOn = DateTime.Now
                                                };
                                                qr_QrCodeRelateService.Instance.Add_Entity(qrrelate);
                                                #endregion

                                                #region 更改二维码类型表
                                                type.QT_CurrentIndex = sceneid;
                                                type.QT_Used = type.QT_Used + 1;
                                                type.QT_Surplus = type.QT_Surplus - 1;
                                                qr_QrCodeTypeService.Instance.Update_Entity(type);
                                                #endregion

                                                msg = "请求成功,二维码：" + resultentity.message.qrcode_url + "code_url:" + resultentity.message.code_url + "" + resultentity.message.id;
                                            }
                                            else
                                            {
                                                msg = "请求失败" + resultentity.errmsg;
                                            }

                                            #endregion

                                            ComLib.LogLib.Log4NetBase.Log("3、====新增单图文NC二维码========新增===NCCode:" + o.nc_code + "::==sceneid==" + resultentity.message.scene_id);
                                            #endregion
                                        }
                                        else
                                        {
                                            #region 编辑more图文信息
                                            var extra = new List<extra>() { 
                                            new extra { 
                                                title = "马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇", 
                                                shortdesc = "一键注册，终身礼遇", 
                                                linkurl = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode="+o.nc_code+"$openId=####", 
                                                picurl = "http://admin.mjn.howjoin.com/images/p1.jpg" 
                                            },
                                            new extra{
                                                title = "不可错过的海量专业孕育课程＆胎教早教资源！", 
                                                shortdesc = "有声资源每周更新，满足妈妈＆宝宝共同需求", 
                                                linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=2$sn=0d2328393e16964b17e11e30bb85378f$scene=1$srcid=06209qW7BXxURv3bBLvzMGHZ", 
                                                picurl = "http://admin.mjn.howjoin.com/images/p2.jpg" 
                                            },
                                            new extra{
                                                title = "【会员福利】奶粉、京东优惠券、巧虎礼包、滴滴红包等你拿,还有……", 
                                                shortdesc = "惊喜连连，准备好了么？", 
                                                linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=3$sn=8478a978359403d68c1452f132455d32$scene=1$srcid=0620AZL0kIEOrjKTGX6Y2emW", 
                                                picurl = "http://admin.mjn.howjoin.com/images/p3.jpg" 
                                            }
                                         };

                                            var message = new message()
                                            {
                                                title = o.name,
                                                remark = o.name + "的二维码",
                                                sacode = "指定NC编号test",
                                                scene_id = sceneid,
                                                extra = extra
                                            };
                                            #endregion

                                            #region 请求vigo 生成二维码
                                            var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
                                            var resultentity = vigo.CreateQrcode(message);
                                            #endregion

                                            while (resultentity.errcode == "12")
                                            {
                                                ComLib.LogLib.Log4NetBase.Log("二维码sceneid重复" + sceneid);
                                                sceneid += 1;
                                                message.scene_id = sceneid;
                                                resultentity = vigo.CreateQrcode(message);
                                            }

                                            #region 将返回的信息保存
                                            if (resultentity.errcode == "00")
                                            {
                                                #region 保存二维码
                                                qr_QrCode qrcode = new qr_QrCode()
                                                {
                                                    id = resultentity.message.id,
                                                    type = 1,
                                                    scene_id = resultentity.message.scene_id,
                                                    sacode = resultentity.message.sacode,
                                                    title = resultentity.message.title,
                                                    remark = resultentity.message.remark,
                                                    code_url = resultentity.message.code_url,
                                                    qrcode_url = resultentity.message.qrcode_url,
                                                    addtime = resultentity.message.addtime,
                                                    status = resultentity.message.status,
                                                    DateModified = DateTime.Now,
                                                    wx_title = "【美赞臣】微信注册",
                                                    wx_content = "注册成为会员，享受更多优惠折扣！",
                                                    wx_url = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode=" + o.nc_code + "$openId=####",
                                                    wx_imgurl = "http://admin.mjn.howjoin.com/images/logo_b.png",
                                                    isdelete = false
                                                };
                                                qr_QrCodeService.Instance.Add_Entity(qrcode);
                                                #endregion

                                                #region 添加关联
                                                qr_QrCodeRelate qrrelate = new qr_QrCodeRelate()
                                                {
                                                    qr_Type = 1,//1是NC
                                                    qr_CodeID = resultentity.message.id,
                                                    qr_Scene_ID = resultentity.message.scene_id,
                                                    rel_Code = o.nc_code,
                                                    IsDelete = false,
                                                    CreateOn = DateTime.Now
                                                };
                                                qr_QrCodeRelateService.Instance.Add_Entity(qrrelate);
                                                #endregion

                                                #region 更改二维码类型表
                                                type.QT_CurrentIndex = sceneid;
                                                type.QT_Used = type.QT_Used + 1;
                                                type.QT_Surplus = type.QT_Surplus - 1;
                                                qr_QrCodeTypeService.Instance.Update_Entity(type);
                                                #endregion
                                            }
                                            #endregion

                                            ComLib.LogLib.Log4NetBase.Log("3、====新增NC二维码========新增===NCCode:" + o.nc_code + "::==sceneid==" + resultentity.message.scene_id);
                                        }
                                        #endregion
                                    }
                                    else
                                    {
                                        #region 使用弃用的

                                        var qr = oldqr[0];

                                        #region 更新二维码

                                        #region 编辑图文信息
                                        var extra = new List<extra>() { 
                                            new extra { 
                                                title = "马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇", 
                                                shortdesc = "一键注册，终身礼遇", 
                                                linkurl = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode="+o.nc_code+"$openId=####", 
                                                picurl = "http://admin.mjn.howjoin.com/images/p1.jpg" 
                                            }
                                        };
                                        var message = new message2()
                                        {
                                            id = qr.id.ToString(),
                                            title = o.name,
                                            remark = o.name + "的二维码",
                                            sacode = o.name + "的二维码",
                                            extra = extra
                                        };
                                        #endregion

                                        var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
                                        var result = vigo.UpdateQrcode(message);

                                        #region 修改弃用的二维码状态为启用
                                        qr.title = o.name;
                                        qr.remark = o.name + "的二维码";
                                        qr.isdelete = false;
                                        qr.DateModified = DateTime.Now;
                                        qr.wx_title = "【美赞臣】微信注册";
                                        qr.wx_content = "注册成为会员，享受更多优惠折扣！";
                                        qr.wx_url = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode=" + o.nc_code + "$openId=####";
                                        qr.wx_imgurl = "http://admin.mjn.howjoin.com/images/logo_b.png";
                                        qr_QrCodeService.Instance.Update_Entity(qr);
                                        #endregion

                                        #endregion

                                        #region 新建关联
                                        qr_QrCodeRelate qrrelate = new qr_QrCodeRelate()
                                        {
                                            qr_Type = 1006,//1006是NC妈妈班
                                            qr_CodeID = qr.id,
                                            qr_Scene_ID = qr.scene_id,
                                            rel_Code = o.nc_code,
                                            IsDelete = false,//启用
                                            CreateOn = DateTime.Now
                                        };
                                        qr_QrCodeRelateService.Instance.Add_Entity(qrrelate);
                                        #endregion

                                        ComLib.LogLib.Log4NetBase.Log("3、====NC为活跃，使用旧的二维码：：：NCCode：：" + o.nc_code + "::==sceneid==::" + qrrelate.qr_Scene_ID);

                                        #endregion
                                    }
                                    #endregion
                                }
                                else
                                {
                                    #region 如果有了二维码，也更新一次
                                    var relate = qr_QrCodeRelateService.Instance.Get_SingleEntity_byWhere(
                                    qr_QrCodeRelate._.qr_Type == 1006 && qr_QrCodeRelate._.IsDelete == false
                                    && qr_QrCodeRelate._.rel_Code == o.nc_code);

                                    if (relate != null)
                                    {
                                        var qrcode = qr_QrCodeService.Instance.Get_SingleEntity_byWhere(qr_QrCode._.id == relate.qr_CodeID);
                                        if (qrcode != null)
                                        {
                                            #region 更新二维码

                                            #region 编辑图文信息
                                            var extra = new List<extra>() { 
                                                new extra {
                                                            title = "EPC测试NC姓名"+o.name+"NCCode"+o.nc_code+"", 
                                                            shortdesc = "EPC测试"+sumnc+"",                               
                                                            picurl = "http://admin.mjn.howjoin.com/images/p1.jpg",
                                                            linkurl = "https://tepclass.meadjohnson.joypaw.com/?channel=3$ncid="+o.nc_code+"$openId=####" 
                                                }
                                            };
                                            var message = new message2()
                                            {
                                                id = qrcode.id.ToString(),
                                                title = o.name,
                                                remark = o.name + "的二维码",
                                                sacode = o.name + "的二维码",
                                                extra = extra
                                            };
                                            #endregion

                                            var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
                                            var result = vigo.UpdateQrcode(message);
                                            #endregion

                                            ComLib.LogLib.Log4NetBase.Log("3、====NC二维码修改：：：NCCode：：" + o.nc_code + "::==sceneid==::" + relate.qr_Scene_ID);
                                        }
                                    }
                                    #endregion
                                }
                                #endregion
                            }
                            else if (o.state == "离职员工")
                            {
                                #region 离职员工的处理

                                var relate = qr_QrCodeRelateService.Instance.Get_SingleEntity_byWhere(
                                    qr_QrCodeRelate._.qr_Type == 1006 && qr_QrCodeRelate._.IsDelete == false
                                    && qr_QrCodeRelate._.rel_Code == o.nc_code);

                                if (relate != null)
                                {
                                    var qrcode = qr_QrCodeService.Instance.Get_SingleEntity_byWhere(qr_QrCode._.id == relate.qr_CodeID);
                                    if (qrcode != null)
                                    {
                                        #region 更新二维码，将二维码改成弃用

                                        #region 编辑图文信息
                                        //linkurl = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?openId=####", 
                                        var extra = new List<extra>() { 
                                            new extra { 
                                                title = "EPC测试NC姓名"+o.name+"NCCode"+o.nc_code+"", 
                                                shortdesc = "EPC测试"+sumnc+"",                               
                                                picurl = "http://admin.mjn.howjoin.com/images/p1.jpg",
                                                linkurl = "https://tepclass.meadjohnson.joypaw.com/?openId=####" 
                                            }
                                        };
                                        var message = new message2()
                                        {
                                            id = qrcode.id.ToString(),
                                            title = o.name,
                                            remark = o.name + "的旧二维码",
                                            sacode = o.name + "的旧二维码",
                                            extra = extra
                                        };
                                        #endregion

                                        var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
                                        var result = vigo.UpdateQrcode(message);

                                        #region 修改的启用二维码状态为弃用
                                        qrcode.isdelete = true;//弃用
                                        qrcode.DateModified = DateTime.Now;
                                        qrcode.wx_title = "";
                                        qrcode.wx_content = "";
                                        qrcode.wx_url = "";
                                        qrcode.wx_imgurl = "";
                                        qr_QrCodeService.Instance.Update_Entity(qrcode);
                                        #endregion

                                        #endregion

                                        #region 修改关联成删除
                                        relate.IsDelete = true;
                                        relate.DateModified = DateTime.Now;
                                        qr_QrCodeRelateService.Instance.Update_Entity(relate);
                                        #endregion

                                        ComLib.LogLib.Log4NetBase.Log("3、====NC离职，将此二维码弃用：：：NCCode：：" + o.nc_code + "::==sceneid==::" + relate.qr_Scene_ID);
                                    }
                                }

                                #endregion
                            }

                            #region 将nc置为已修改
                            o.isneedupdate = false;
                            nc_ncuserService.Instance.Update_Entity(o);
                            #endregion
                        }
                        catch (Exception ex)
                        {
                            ComLib.LogLib.Log4NetBase.Log("更新一个二维码错误::::" + ex.Message + ex.StackTrace);
                        }
                    });

                }
                #endregion
            }
            else
            {
                #region 自定义NCcode（需要修改数据库指定NC的 isneedupdate == 1）
                var nclist = nc_ncuserService.Instance.Get_Entity_byWhere(Entity.nc_ncuser._.isneedupdate == true, null);
                ComLib.LogLib.Log4NetBase.Log("3、==========================创建NC二维码===========数量:" + nclist.Count);
                nclist.ForEach(o =>
                {
                    try
                    {
                        #region 测试新增NC
                        if (o.state == "活跃员工" || o.state == "休眠员工")
                        {
                            #region 活跃员工的处理 测试创建NC

                            //var has = qr_QrCodeRelateService.Instance.Get_IsExist_ByWhere(
                            //    qr_QrCodeRelate._.qr_Type == 1 && qr_QrCodeRelate._.IsDelete == false
                            //    && qr_QrCodeRelate._.rel_Code == o.nc_code);
                            bool has = false;
                            if (has == false)
                            {
                                #region 如果在关联表没有NC对应的记录，就新增二维码

                                //查询是否有以前弃用的二维码
                                //var oldqr = qr_QrCodeService.Instance.Get_Entitys_ByTop(1, qr_QrCode._.type == 1 &&
                                //    qr_QrCode._.isdelete == true, qr_QrCode._.scene_id.Asc);
                                //var oldqr = null;
                                bool isold = false;
                                if (isold == false)
                                {
                                    #region 使用新的

                                    #region get sceneid
                                    var sceneid = 99912;
                                    qr_QrCodeType type = qr_QrCodeTypeService.Instance.Get_SingleEntity_byWhere(qr_QrCodeType._.QrCodeTypeID == 1006);
                                    //sceneid = type.QT_CurrentIndex.ConvertTo<int>() + 1;
                                    int sceneid2 = Convert.ToInt32(sceneid);
                                    #endregion

                                    if (actiontype == "one")
                                    {
                                        #region one
                                        #region 编辑单图文信息
                                        var extra = new List<extra>() { 
                                  new extra { 
                                    title = "马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇", 
                                    shortdesc = "一键注册，终身礼遇",                                
                                    picurl = "http://admin.mjn.howjoin.com/images/p1.jpg",
                                    linkurl = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode=" + o.nc_code + "$openId=####", 
                                      }
                                    };
                                        //var extra = "[{\"title\":\"" + "一键注册，终身礼遇"
                                        //              + "\",\"shortdesc\":\"" + "马上加入美赞臣A+妈妈会，尊享360°孕育礼遇"20000149
                                        //              + "\",\"picurl\":\"" + "http://admin.mjn.howjoin.com/images/p1.jpg"
                                        //              + "\",\"linkurl\":\"" + "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode=20000149$openId=####"
                                        //              + "\"}]";
                                        // {"extra":[{"linkurl":"http:\/\/crmweb.meadjohnson.com.cn\/mjnwx\/client_phone.action?regSubChannel=188$NCCode=20000149$openId=####","picurl":"http:\/\/admin.mjn.howjoin.com\/images\/p1.jpg","shortdesc":"一键注册，终身礼遇","title":"马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇"}],"title":"TestQrCodeOne","remark":"Test","sacode":"","scene_id":99900}
                                        //var message = "{\"title\":\"" + "TestQrCodeOne"
                                        //               + "\",\"remark\":\"" + "Test"
                                        //               + "\",\"sacode\":\"" + ""
                                        //               + "\",\"scene_id\":" + "99900"
                                        //               + ",\"extra\":" + extra
                                        //               + "}";

                                        var message = new message()
                                        {
                                            //title = o.name,
                                            //remark = o.name + "的二维码",
                                            //sacode = "test",
                                            //scene_id = sceneid.ToString(),
                                            //extra = extra
                                            title = "TestQrCodeOne",
                                            remark = "Test",
                                            sacode = "",
                                            scene_id = sceneid,
                                            extra = extra
                                        };
                                        #endregion


                                        #region 请求vigo 生成二维码
                                        var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
                                        var resultentity = vigo.CreateQrcode(message);
                                        #endregion

                                        while (resultentity.errcode == "12")
                                        {
                                            ComLib.LogLib.Log4NetBase.Log("二维码sceneid重复" + sceneid);
                                            sceneid -= 1;
                                            message.scene_id = sceneid;
                                            resultentity = vigo.CreateQrcode(message);
                                        }
                                        while (resultentity.errcode == "02")
                                        {
                                            ComLib.LogLib.Log4NetBase.Log(resultentity.errmsg + sceneid);
                                            sceneid -= 1;
                                            message.scene_id = sceneid;
                                            resultentity = vigo.CreateQrcode(message);
                                        }
                                        #region 将返回的信息保存
                                        if (resultentity.errcode == "00")
                                        {
                                            #region 保存二维码
                                            qr_QrCode qrcode = new qr_QrCode()
                                            {
                                                id = resultentity.message.id,
                                                type = 1006,
                                                scene_id = resultentity.message.scene_id,
                                                sacode = resultentity.message.sacode,
                                                title = resultentity.message.title,
                                                remark = resultentity.message.remark,
                                                code_url = resultentity.message.code_url,
                                                qrcode_url = resultentity.message.qrcode_url,
                                                addtime = resultentity.message.addtime,
                                                status = resultentity.message.status,
                                                DateModified = DateTime.Now,
                                                wx_title = "【美赞臣】微信注册",
                                                wx_content = "注册成为会员，享受更多优惠折扣！",
                                                wx_url = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode=" + o.nc_code + "$openId=####",
                                                wx_imgurl = "http://admin.mjn.howjoin.com/images/logo_b.png",
                                                isdelete = false
                                            };
                                            qr_QrCodeService.Instance.Add_Entity(qrcode);
                                            #endregion

                                            #region 添加关联
                                            qr_QrCodeRelate qrrelate = new qr_QrCodeRelate()
                                            {
                                                qr_Type = 1006,//1006是NC妈妈班
                                                qr_CodeID = resultentity.message.id,
                                                qr_Scene_ID = resultentity.message.scene_id,
                                                rel_Code = o.nc_code,
                                                IsDelete = false,
                                                CreateOn = DateTime.Now
                                            };
                                            qr_QrCodeRelateService.Instance.Add_Entity(qrrelate);
                                            #endregion

                                            #region 更改二维码类型表
                                            type.QT_CurrentIndex = sceneid;
                                            type.QT_Used = type.QT_Used + 1;
                                            type.QT_Surplus = type.QT_Surplus - 1;
                                            qr_QrCodeTypeService.Instance.Update_Entity(type);
                                            #endregion

                                            msg = "请求成功,二维码：" + resultentity.message.qrcode_url + "code_url:" + resultentity.message.code_url + "" + resultentity.message.id;
                                        }
                                        else
                                        {
                                            msg = "请求失败" + resultentity.errmsg;
                                        }

                                        #endregion

                                        ComLib.LogLib.Log4NetBase.Log("3、====新增单图文NC二维码========新增===NCCode:" + o.nc_code + "::==sceneid==" + resultentity.message.scene_id);
                                        #endregion
                                    }
                                    else
                                    {
                                        #region more
                                        #region 编辑多图文信息
                                        var extra = new List<extra>() { 
                                new extra { 
                                    title = "马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇", 
                                    shortdesc = "一键注册，终身礼遇", 
                                    linkurl = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode="+o.nc_code+"$openId=####", 
                                    picurl = "http://admin.mjn.howjoin.com/images/p1.jpg" 
                                },
                                new extra{
                                    title = "不可错过的海量专业孕育课程＆胎教早教资源！", 
                                    shortdesc = "有声资源每周更新，满足妈妈＆宝宝共同需求", 
                                    linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=2$sn=0d2328393e16964b17e11e30bb85378f$scene=1$srcid=06209qW7BXxURv3bBLvzMGHZ", 
                                    picurl = "http://admin.mjn.howjoin.com/images/p2.jpg" 
                                },
                                new extra{
                                    title = "【会员福利】奶粉、京东优惠券、巧虎礼包、滴滴红包等你拿,还有……", 
                                    shortdesc = "惊喜连连，准备好了么？", 
                                    linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=3$sn=8478a978359403d68c1452f132455d32$scene=1$srcid=0620AZL0kIEOrjKTGX6Y2emW", 
                                    picurl = "http://admin.mjn.howjoin.com/images/p3.jpg" 
                                }
                            };
                                        var message = new message()
                                        {
                                            title = "TestQrCodeOne",
                                            remark = "Test",
                                            sacode = "",
                                            scene_id = sceneid,
                                            extra = extra
                                        };
                                        #endregion

                                        #region 请求vigo 生成二维码
                                        var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
                                        var resultentity = vigo.CreateQrcode(message);
                                        #endregion

                                        while (resultentity.errcode == "12")
                                        {
                                            ComLib.LogLib.Log4NetBase.Log("二维码sceneid重复" + sceneid);
                                            sceneid -= 1;
                                            message.scene_id = sceneid;
                                            resultentity = vigo.CreateQrcode(message);
                                        }

                                        #region 将返回的信息保存
                                        if (resultentity.errcode == "00")
                                        {
                                            #region 保存二维码
                                            qr_QrCode qrcode = new qr_QrCode()
                                            {
                                                id = resultentity.message.id,
                                                type = 1006,
                                                scene_id = resultentity.message.scene_id,
                                                sacode = resultentity.message.sacode,
                                                title = resultentity.message.title,
                                                remark = resultentity.message.remark,
                                                code_url = resultentity.message.code_url,
                                                qrcode_url = resultentity.message.qrcode_url,
                                                addtime = resultentity.message.addtime,
                                                status = resultentity.message.status,
                                                DateModified = DateTime.Now,
                                                wx_title = "【美赞臣】微信注册",
                                                wx_content = "注册成为会员，享受更多优惠折扣！",
                                                wx_url = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode=" + o.nc_code + "$openId=####",
                                                wx_imgurl = "http://admin.mjn.howjoin.com/images/logo_b.png",
                                                isdelete = false
                                            };
                                            qr_QrCodeService.Instance.Add_Entity(qrcode);
                                            #endregion

                                            #region 添加关联
                                            qr_QrCodeRelate qrrelate = new qr_QrCodeRelate()
                                            {
                                                qr_Type = 1006,//1006是NC妈妈班
                                                qr_CodeID = resultentity.message.id,
                                                qr_Scene_ID = resultentity.message.scene_id,
                                                rel_Code = o.nc_code,
                                                IsDelete = false,
                                                CreateOn = DateTime.Now
                                            };
                                            qr_QrCodeRelateService.Instance.Add_Entity(qrrelate);
                                            #endregion

                                            #region 更改二维码类型表
                                            type.QT_CurrentIndex = sceneid;
                                            type.QT_Used = type.QT_Used + 1;
                                            type.QT_Surplus = type.QT_Surplus - 1;
                                            qr_QrCodeTypeService.Instance.Update_Entity(type);
                                            #endregion

                                            msg = "请求成功,二维码：" + resultentity.message.qrcode_url + "/code_url:" + resultentity.message.code_url + "" + resultentity.message.id;
                                        }
                                        else
                                        {
                                            msg = "请求失败" + resultentity.errmsg;
                                        }

                                        #endregion

                                        ComLib.LogLib.Log4NetBase.Log("3、====新增单图文NC二维码========新增===NCCode:" + o.nc_code + "::==sceneid==" + resultentity.message.scene_id);
                                        #endregion
                                    }
                                    #endregion
                                }
                                else
                                {

                                }
                                #endregion
                            }
                            else
                            {
                                #region 如果有了二维码，也更新一次
                                var relate = qr_QrCodeRelateService.Instance.Get_SingleEntity_byWhere(
                                qr_QrCodeRelate._.qr_Type == 1 && qr_QrCodeRelate._.IsDelete == false
                                && qr_QrCodeRelate._.rel_Code == o.nc_code);

                                if (relate != null)
                                {
                                    var qrcode = qr_QrCodeService.Instance.Get_SingleEntity_byWhere(qr_QrCode._.id == relate.qr_CodeID);
                                    if (qrcode != null)
                                    {
                                        #region 更新二维码

                                        #region 编辑图文信息
                                        var extra = new List<extra>() { 
                                        new extra { 
                                            title = "马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇", 
                                            shortdesc = "一键注册，终身礼遇", 
                                            linkurl = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode="+o.nc_code+"$openId=####", 
                                            picurl = "http://admin.mjn.howjoin.com/images/p1.jpg" 
                                        },
                                        new extra{
                                            title = "不可错过的海量专业孕育课程＆胎教早教资源！", 
                                            shortdesc = "有声资源每周更新，满足妈妈＆宝宝共同需求", 
                                            linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=2$sn=0d2328393e16964b17e11e30bb85378f$scene=1$srcid=06209qW7BXxURv3bBLvzMGHZ", 
                                            picurl = "http://admin.mjn.howjoin.com/images/p2.jpg" 
                                        },
                                        new extra{
                                            title = "【会员福利】奶粉、京东优惠券、巧虎礼包、滴滴红包等你拿,还有……", 
                                            shortdesc = "惊喜连连，准备好了么？", 
                                            linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=3$sn=8478a978359403d68c1452f132455d32$scene=1$srcid=0620AZL0kIEOrjKTGX6Y2emW", 
                                            picurl = "http://admin.mjn.howjoin.com/images/p3.jpg" 
                                        }
                                    };
                                        var message = new message2()
                                        {
                                            id = qrcode.id.ToString(),
                                            title = o.name,
                                            remark = o.name + "的二维码",
                                            sacode = o.name + "的二维码",
                                            extra = extra
                                        };
                                        #endregion

                                        var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
                                        var result = vigo.UpdateQrcode(message);
                                        //if (result.errcode == "00")
                                        msg = "修改成功" + result.ToString();
                                        #endregion

                                        ComLib.LogLib.Log4NetBase.Log("3、====NC二维码修改：：：NCCode：：" + o.nc_code + "::==sceneid==::" + relate.qr_Scene_ID);
                                    }
                                }
                                #endregion
                            }
                            #endregion

                        }
                        else
                        {

                        }

                        #region 将nc置为已修改
                        o.isneedupdate = false;
                        nc_ncuserService.Instance.Update_Entity(o);
                        #endregion
                        #endregion


                    }
                    catch (Exception ex)
                    {
                        ComLib.LogLib.Log4NetBase.Log("更新一个二维码错误::::" + ex.Message + ex.StackTrace);
                    }
                });
                #endregion

            }

            return msg;
        }
        //测试NC妈妈班更新二维码
        public string Sync_QrCode(string nccode)
        {

            string msg = "";
            #region 先得到所有的需要更新二维码的NC   905
            var nclist = nc_ncuserService.Instance.Get_Entity_byWhere(Entity.nc_ncuser._.isneedupdate == true, null);
            ComLib.LogLib.Log4NetBase.Log("3、==========================创建NC二维码===========数量:" + nclist.Count);
            nclist.ForEach(o =>
            {
                try
                {                  
                    #region 更新二维码
                    var entity = nc_ncuserService.Instance.Get_SingleEntity_byWhere(ShareMarking.Entity.nc_ncuser._.nc_code == "905");
                    if (entity != null)
                    {
                        var relate = qr_QrCodeRelateService.Instance.Get_SingleEntity_byWhere(
                            qr_QrCodeRelate._.qr_Type == 1006 && qr_QrCodeRelate._.IsDelete == false
                            && qr_QrCodeRelate._.rel_Code == entity.nc_code);
                        if (relate != null)
                        {
                            var qrcode = qr_QrCodeService.Instance.Get_SingleEntity_byWhere(qr_QrCode._.id == relate.qr_CodeID);//"15968"
                            if (qrcode != null)
                            {
                                #region 更新二维码

                                #region 编辑图文信息
                                //title = "马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇", 
                                // title = "不可错过的海量专业孕育课程＆胎教早教资源！", 
                                var extra = new List<extra>() { 
                                        new extra { 
                                            //title = "2016-12-01测试更新A", 
                                            title = "马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇"+DateTime.Now,
                                            shortdesc = "一键注册，终身礼遇", 
                                            linkurl = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode="+o.nc_code+"$openId=####", 
                                            picurl = "http://admin.mjn.howjoin.com/images/p1.jpg" 
                                        },
                                        new extra{
                                            //title = "2016-12-01测试更新B", 
                                            title = "不可错过的海量专业孕育课程＆胎教早教资源！"+DateTime.Now, 
                                            shortdesc = "有声资源每周更新，满足妈妈＆宝宝共同需求", 
                                            linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=2$sn=0d2328393e16964b17e11e30bb85378f$scene=1$srcid=06209qW7BXxURv3bBLvzMGHZ", 
                                            picurl = "http://admin.mjn.howjoin.com/images/p2.jpg" 
                                        },
                                        new extra{
                                            title = "【会员福利】奶粉、京东优惠券、巧虎礼包、滴滴红包等你拿,还有……"+DateTime.Now, 
                                            //title = "2016-12-01测试更新C",
                                            shortdesc = "惊喜连连，准备好了么？", 
                                            linkurl = "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=3$sn=8478a978359403d68c1452f132455d32$scene=1$srcid=0620AZL0kIEOrjKTGX6Y2emW", 
                                            picurl = "http://admin.mjn.howjoin.com/images/p3.jpg" 
                                        }
                                };
                                var message = new message2()
                                {
                                    //id = qrcode.id.ToString(),
                                    //title = entity.name,
                                    //remark = entity.name + "多图文",
                                    //sacode = entity.name + "多图文",
                                    //extra = extra
                                    id = qrcode.id.ToString(),
                                    title = "TestQrCodeMore",
                                    remark = "Test",
                                    sacode = "测试更新多图文",
                                    extra = extra
                                };
                                #endregion

                                var vigo = new Qrcode_vigo(this.httpstr, this.signserect);
                                var info = vigo.UpdateQrcode(message);
                                msg = info;
                                #endregion

                            }
                        }
                    }
                    #endregion
                }
                catch (Exception ex)
                {
                    ComLib.LogLib.Log4NetBase.Log("更新一个二维码错误::::" + ex.Message + ex.StackTrace);
                }
            });
            #endregion
            return msg;
        }
        #endregion
    }
}
